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
#2
A couple of things to look at:
I would suggest three processes, running the timer separately and
notifying the other processes as required.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to process tasks as they complete when using TaskGroup? odigity 2 959 Sep-17-2024, 03:42 PM
Last Post: odigity
  Managing recursive tasks in celery. vamix 1 812 Sep-04-2024, 06:36 PM
Last Post: deanhystad
  drawing a table with the status of tasks in each thread pyfoo 3 1,408 Mar-01-2024, 09:29 AM
Last Post: nerdyaks
  How to script repetitive tasks in dynaform using python BenneGrus 0 1,958 Dec-22-2021, 08:36 AM
Last Post: BenneGrus
  asyncio: executing tasks + httpserver freebsdd 2 3,585 Aug-29-2020, 09:50 PM
Last Post: freebsdd
  Watching for inputs from multiple sources concurrently anakaine 4 3,730 Mar-03-2019, 07:38 PM
Last Post: woooee
  How to add asynchronous tasks as they are needed? AlekseyPython 2 5,522 Jan-11-2019, 02:58 AM
Last Post: AlekseyPython
  How I can limit quantity of parallel executable tasks in asyncio? AlekseyPython 1 3,044 Oct-24-2018, 10:22 AM
Last Post: AlekseyPython
  reading 2 files concurrently Skaperen 3 4,016 Jun-01-2018, 07:07 AM
Last Post: Skaperen
  Python - Make Json objects to work concurrently through Threads? WeInThis 0 3,359 Sep-22-2017, 11:31 AM
Last Post: WeInThis

Forum Jump:

User Panel Messages

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