Small Example for pyserial using Threading - python

Can anyone please give me a small and simple example on how to use threading with pyserial communication.
I am googling for over three days and I am still illeterate and I dont even have a working piece of code which integrate both of them
Basically I am aiming to use threading in this scenario:
Have a serial communication continuously go on in the back ground to attain certain value (say A) from an MCU.
Stop attaining value A - then attain value B...and start continuously attaining value A again.
You can find some basic code here.
import threading
import time
import sys
import serial
import os
import time
def Task1(ser):
while 1:
print "Inside Thread 1"
ser.write('\x5A\x03\x02\x02\x02\x09') # Byte ArrayTo Control a MicroProcessing Unit
b = ser.read(7)
print b.encode('hex')
print "Thread 1 still going on"
time.sleep(1)
def Task2(ser):
print "Inside Thread 2"
print "I stopped Task 1 to start and execute Thread 2"
ser.write('x5A\x03\x02\x08\x02\x0F')
c = ser.read(7)
print c.encode('hex')
print "Thread 2 complete"
def Main():
ser = serial.Serial(3, 11520)
t1 = threading.Thread(target = Task1, args=[ser])
t2 = threading.Thread(target = Task2, args=[ser])
print "Starting Thread 1"
t1.start()
print "Starting Thread 2"
t2.start()
print "=== exiting ==="
ser.close()
if __name__ == '__main__':
Main()

There's no factual basis for the claim by Task2:
print "I stopped Task 1 to start and execute Thread 2"
Your implementation starts one thread then immediately starts the other without stopping the first. This means that the ser.read and ser.write commands could interfere with each other.
The simplest thing you could do to address this is to introduce variables that allow communication:
import sys
import os
import time
import threading
thread_flag = None
def Report(s):
print s
sys.stdout.flush() # helps to ensure messages from different threads appear in the right order
def Stop():
global thread_flag
thread_flag = 'stop'
def Task1(ser):
Report("Inside Thread 1")
global thread_flag
thread_flag = 'go'
while True:
Report("Thread 1 waiting for permission to read")
while thread_flag != 'go': time.sleep( 0.001 )
while thread_flag == 'go':
Report("Thread 1 is reading")
#ser.write('\x5A\x03\x02\x02\x02\x09') # Byte ArrayTo Control a MicroProcessing Unit
#b = ser.read(7)
#Report(b.encode('hex'))
time.sleep(1)
if thread_flag == 'stop': break
else: thread_flag = 'paused' # signals that the inner loop is done
Report("Thread 1 complete")
def Task2(ser):
Report("Inside Thread 2")
global thread_flag
thread_flag = 'pause' # signals Task1 to pause
while thread_flag != 'paused': time.sleep(0.001) # waits for Task1 inner loop to exit
Report("I stopped Task 1 to start and execute Thread 2")
#ser.write('x5A\x03\x02\x08\x02\x0F')
#c = ser.read(7)
#Report(c.encode('hex'))
thread_flag = 'go' # signals Thread 1 to resume
Report("Thread 2 complete")
def Main():
ser = None # serial.Serial(3, 11520)
t1 = threading.Thread(target = Task1, args=[ser])
t2 = threading.Thread(target = Task2, args=[ser])
Report("Starting Thread 1")
t1.start()
time.sleep(3)
Report("Starting Thread 2")
t2.start()
if __name__ == '__main__':
Main()
That approach uses a global variable, which is often frowned upon. You could instead make Task1 and Task2 methods of an object self that carries both self.ser and self.thread_flag as attributes.
For any inter-thread communication more complex than this, you need to investigate locks, mutexes and semaphores (e.g. threading.Lock)

Related

How to add a pause in python without disrupting other code

I'm attempting to make a script that keeps the times of each boss in a game through text.
an example would be:
if line == 'boss1 down':
print('boss1 timer set for 10 seconds')
time.sleep(10)
print("boss1 due")
if line == 'boss2 down':
print('boss2 timer set for 15 seconds')
time.sleep(15)
print("boss2 due")
However, the clear issue is that only one boss can be timed at a time. Is there a function I can use that won't disrupt the code and allow me to time multiple at a given time?
You can use the Thread class from the built-in threading module:
from threading import Thread
import time
def timer(num, secs, line):
if line == f'boss{num} down':
print(f'boss{num} timer set for {secs} seconds ')
time.sleep(secs)
print(f"boss{num} due")
boss1 = Thread(target=timer, args=(1, 10, "boss1 down"))
boss2 = Thread(target=timer, args=(2, 15, "boss2 down"))
boss1.start()
boss2.start()
Output:
boss1 timer set for 10 seconds boss2 timer set for 15 seconds
boss1 due
boss2 due
You can use Asynchronous function :
import asyncio
async def boss1_down():
print('boss1 timer set for 10 seconds')
await asyncio.sleep(10)
print("boss1 due")
asyncio.run(boss1_down())
, add arguments to the function for timers and boss.
As #Mayank mentioned, you can also use threads, which are a bit more complex to settle but you can control them (you cannot stop or wait an async function).
From this answer
You can use threading to do asynchronous tasks.
from threading import Thread
from time import sleep
def threaded_function(line):
if line == 'boss1 down':
print('boss1 timer set for 10 seconds')
time.sleep(10)
print("boss1 due")
if line == 'boss2 down':
print('boss2 timer set for 15 seconds')
time.sleep(15)
print("boss2 due")
if __name__ == "__main__":
thread1 = Thread(target = threaded_function, args= 'boss1 down')
thread1.start()
thread1.join()
thread2 = Thread(target = threaded_function, args= 'boss2 down')
thread2.start()
thread2.join()
print("thread finished...exiting")

Threading infinity loops and sync problem

I am getting headache, trying to figure out how to deal with 2 threads in Python.
My Idea was to create one thread for blinking LEDs, that have infinite while loop, and when global variable changes to True,it starts to blink etc, but it is always in infinite loop.
other main thread, will be waiting for signal to blink, but also if the signal is while blinking, it will reset global blinking timer, so it has to be in separate thread.
Problem I am having, when I start LEDs thread from main thread, because LED thread has infinite loop, it stops main thread from running any further than flashThread.start()
if __name__ == "__main__":
print('starting main...')
flashThread = threading.Thread(name='SFlashing', daemon = True, target=Flash)
flashThread.start()
while(True):
print('running main...')
time.sleep(2)
it never gets to running main...
Flash() is just infinite while loop and depending on global variables it does stuff...
Here is an example of a main thread printing running main... + a parallel thread incrementing a variable count, based on your explanations. Your code should work, maybe could you give us more information about the Flash loop so we can help you ?
import threading
import time
def Flash():
count = 0
while True:
print("count", count)
count += 1
time.sleep(1)
print('starting main...')
flashThread = threading.Thread(name='SFlashing', daemon = True, target=Flash)
flashThread.start()
while(True):
print('running main...')
time.sleep(2)
Output:
starting main...
count 0
running main...
count 1
running main...
count 2
count 3
running main...
** EDIT **
Here there is no more time.sleep()in Flash, until the global variable totois changed in the main loop.
import threading
import time
global toto
toto = 0
def Flash():
global toto
count = 0
while True:
print("count", count)
count += 1
if toto == 2:
print("YEEHA")
time.sleep(2)
print('starting main...')
flashThread = threading.Thread(name='SFlashing', daemon = True, target=Flash)
flashThread.start()
while(True):
print('running main...')
time.sleep(1)
toto = 2

How to interrupt input after 30 seconds and quit from program? - automatic logout - Python

I would like to make automatic logout after 30 seconds.
Program waits for user to input something and after 30 seconds I would like program to automatically shut down.
I have something like this:
import sys, time, os
def start_controller(user):
start = time.time()
PERIOD_OF_TIME = 30
os.system('clear')
print_menu() #printing menu
choice = get_choice() #get input from view model
while choice != "0":
os.system('clear')
if choice == "1":
start += PERIOD_OF_TIME
print_student_list(Student.student_list,AllAttendance.all_attendance_list)
if time.time() > start + PERIOD_OF_TIME:
os.system("clear")
print('logout')
Database.save_all_data_to_csv()
sys.exit()
Here's a simple example of using threads to get and process user input with a timeout.
We create a Timer thread to perform the timeout function, and wait for the user input in a daemon thread. If the user supplies an input string within the nominated delay period then the Timer is cancelled, otherwise the Timer will set the finished Event to break the while loop. If you need to do any final cleanup, you can do that after the while loop.
from threading import Thread, Timer, Event
def process_input(timer):
s = input('> ')
timer.cancel()
print(s.upper())
delay = 30
finished = Event()
while not finished.isSet():
timer = Timer(delay, finished.set)
worker = Thread(target=process_input, args=(timer,))
worker.setDaemon(True)
worker.start()
timer.start()
timer.join()

Daemon thread not exiting despite main program finishing

I've already referred to this thread, but it seems to be outdated
and there doesn't seem to be a clean explanation
Python daemon thread does not exit when parent thread exits
I'm running python 3.6 and trying to run the script from either IDLE or Spyder IDE.
Here is my code:
import threading
import time
total = 4
def creates_items():
global total
for i in range(10):
time.sleep(2)
print('added item')
total += 1
print('creation is done')
def creates_items_2():
global total
for i in range(7):
time.sleep(1)
print('added item')
total += 1
print('creation is done')
def limits_items():
#print('finished sleeping')
global total
while True:
if total > 5:
print ('overload')
total -= 3
print('subtracted 3')
else:
time.sleep(1)
print('waiting')
limitor = threading.Thread(target = limits_items, daemon = True)
creator1 = threading.Thread(target = creates_items)
creator2 = threading.Thread(target = creates_items_2)
print(limitor.isDaemon())
creator1.start()
creator2.start()
limitor.start()
creator1.join()
creator2.join()
print('our ending value of total is' , total)
limitor thread doesn't seem to be ending despite being a daemon thread.
Is this a way to get this working from IDLE or Spyder?
Thanks.
I had the same Problem and solved it by using multiprocessing instead of threading:
from multiprocessing import Process
import multiprocessing
from time import sleep
def daemon_thread():
for _ in range(10):
sleep(1)
print("Daemon")
if __name__ == '__main__':
multiprocessing.freeze_support()
sub_process = Process(target = daemon_thread, daemon = True)
sub_process.start()
print("Exiting Main")
I haven't yet really understood why I need the call to freeze_support() but it makes the code work.

Causing a thread to stop while stuck within a while loop?

Is is possible to stop a thread prematurely when it is stuck inside a while loop? Below is my sample code, which runs correctly, since each time it calls loop_thread it will check to see if the threading.Event() flag is set. When attempting to run the code for a file that processes information much longer than each second, there is no way to stop the entire function from continuing its execution until the next iteration. For example, if I run dld_img_thread, it takes about 5 minutes to complete its execution and recheck the while loop to see if should proceed. What I want to have happen is kill the dld_img_thread at a time shorter than 5 minutes (e.g. 1 minute). I don't care if the data is lost, just that the thread stops before the function finishes execution. Thank you
import threading, time, pythoncom, read_mt0
import powerfail_debugport_reader as pf_dbg_rdr
import powerfail_firmware_downloader as pf_fwdld
def loop_thread(thread_name, thread_event):
loopCnt = 0
print "\nstarting {}".format(thread_name)
print "is {0} alive? {1}\n".format(thread_name, L00P_thread.is_alive())
while not thread_event.is_set():
print("value of loopCnt = {}".format(loopCnt))
loopCnt += 1
time.sleep(1)
print('stopping {}\n'.format(thread_name))
def image_dld(thread_name, thread_event):
pythoncom.CoInitializeEx(pythoncom.COINIT_MULTITHREADED)
print "\nstarting {}".format(thread_name)
print "is {0} alive? {1}\n".format(thread_name, dld_img_thread.is_alive())
while not thread_event.is_set():
pf_fwdld.power_fail_test()
print('stopping {}'.format(thread_name))
def debug_port_thread(thread_name, thread_event):
pythoncom.CoInitializeEx(pythoncom.COINIT_MULTITHREADED)
print "\nstarting {}".format(thread_name)
print "is {0} alive? {1}\n".format(thread_name, debug_thread.is_alive())
pf_dbg_rdr.debug_port_reader()
print('\nstopping {}'.format(thread_name))
def main():
global L00P_thread, debug_thread
pf_dbg_rdr.samurai_event = threading.Event()
L00P_thread = threading.Thread(target=loop_thread, \
args=('L00P_thread', pf_dbg_rdr.samurai_event))
dld_img_thread = threading.Thread(target=image_dld, \
args=('image_download', pf_dbg_rdr.samurai_event))
debug_thread = threading.Thread(target=debug_port_thread, \
args=('debug_port_reader', pf_dbg_rdr.samurai_event))
L00P_thread.start()
dld_img_thread.start()
debug_thread.start()
debug_thread.join()
if __name__ == '__main__':
main()
print('processes stopped')
print "Exiting Main Thread"
Use a second variable in your while condition that you can change once your timeout is reached.
For example:
shouldRun = True
while not thread_event.is_set() and shouldRun:
print("value of loopCnt = {}".format(loopCnt))
loopCnt += 1
time.sleep(1)
if loopCnt > 60: shouldRun = False
would stop after 60 iterations (about 60 seconds given you sleep for 1 second on each iteration).

Categories

Resources