multithreading - Python Pyserial - Threading -


python 2.7

this code have. please tell me whats wrong. beast come after studying threading on 2 days continuously.

the serial communications work when dont use threading.

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 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 start , 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() 

you not syncing threads. suggest putting ser object global namespace , using lock, mutex or semaphore prevent 2 threads accessing single ser object @ same time.

python module of week explains best here


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

python 3.5 - Pyqtgraph string in x tick -