Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
run two tasks concurrently
#1
Hello, I just got a Pi, that what motivated me to learn Python. I am a absolute newbies when it come to python programming,  I joint this forum.

I am making an app I have a script to read data to Pi with serial from sensors in Aduino,
It is a very simple script:
import serial, time

ser = serial.Serial('/dev/ttyUSB0', 9600)

while True :
   data = ser.readline()
   print str(data)
   time.sleep(1)
ser.close()
now besides reading data. the Pi also control the a timed relay relay to turn things on and off, so I made a timer script:
import RPi.GPIO as GPIO
import time
#GPIO.setmode(GPIO.BCM)
GPIO.setmode(GPIO.BOARD)

GPIO.setwarnings(False)
while True: 
   GPIO.setup(40,GPIO.OUT)
   print ("LED on")
   GPIO.output(40,GPIO.HIGH)
   time.sleep(1) # Insert here?
   print ("LED off")
   GPIO.output(40,GPIO.LOW)
   time.sleep(1) # Insert here?
   

GPIO.cleanup() 
Now my trouble is (conciliate the two scripts into one) keeping the timer running while reading data concurrently. How does python do that? Do I need to create a a dedicated thread to run them or is there a sinpler way to do it. Thanks.
Reply


Messages In This Thread
run two tasks concurrently - by tony1812 - Jul-24-2017, 02:58 PM
RE: run two tasks concurrently - by Larz60+ - Jul-24-2017, 05:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  drawing a table with the status of tasks in each thread pyfoo 3 355 Mar-01-2024, 09:29 AM
Last Post: nerdyaks
  How to script repetitive tasks in dynaform using python BenneGrus 0 1,305 Dec-22-2021, 08:36 AM
Last Post: BenneGrus
  asyncio: executing tasks + httpserver freebsdd 2 2,581 Aug-29-2020, 09:50 PM
Last Post: freebsdd
  Watching for inputs from multiple sources concurrently anakaine 4 2,762 Mar-03-2019, 07:38 PM
Last Post: woooee
  How to add asynchronous tasks as they are needed? AlekseyPython 2 3,936 Jan-11-2019, 02:58 AM
Last Post: AlekseyPython
  How I can limit quantity of parallel executable tasks in asyncio? AlekseyPython 1 2,396 Oct-24-2018, 10:22 AM
Last Post: AlekseyPython
  reading 2 files concurrently Skaperen 3 3,188 Jun-01-2018, 07:07 AM
Last Post: Skaperen
  Python - Make Json objects to work concurrently through Threads? WeInThis 0 2,587 Sep-22-2017, 11:31 AM
Last Post: WeInThis
  Tasks for Python Lamon112 2 33,472 Jan-13-2017, 03:32 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020