multithreading - Python threads not running in parallel -
currently trying have 2 threads running in parallel, seems happening first thread started 1 runs.
my code listed below, more complicated in end, example code exhibits exact same behaviour.
if start first thread first, command line repeatedly print "here", never "here2", , vice versa.
example output:
attempt handshake failed attempt handshake failed attempt handshake failed here2 here2 here2 here2 here2 here2 here2 here2 here2 here2 here2 and code:
import serial import time import threading multiprocessing import process datetime import datetime datetime import timedelta tableapps import * #temporary main function def main(ttl): s = pyserial(ttl); count = 0; inc = 10; x = ''; fac = none; #changed '' singleton "none", it's coolest way check nothingness -abe while 1: if(s.teensyready == false): time.sleep(1); print "attempt handshake failed"; s.handshake(); else: if(fac == none): fac = facade(s); thread = threading.thread(gettouchdata(fac)); thread2 = threading.thread(senddata(fac)); thread2.start(); thread.start(); if(s.teensyready == false): print fac.s.teensyready; thread.join(); thread2.join(); def gettouchdata(fac): while 1: print "here2"; time.sleep(0.1); def senddata(fac): while 1: print "here"; time.sleep(0.1); thanks all!
edit: roippi able implement these threads simultaneously, after few seconds of running, 1 thread seems dominate , other doesn't appear within thread longer. in short like
here here2 here here2 here here2 here here2 here here here here here here here here here here ... edit 2:
ok, seemed solve follow-up issue. within while loop changed condition 'threadstarted' determine whether or not start thread. ontop of this, ran module in command line , not idle.
that being said, after incorporated rest of code, running in idle worked fine. weird.
threading.thread(gettouchdata(fac)) this not proper way call thread. thread needs receive callable target kwarg, , need pass args in tuple args kwarg. altogether:
threading.thread(target=gettouchdata, args=(fac,)) per docs:
class threading.thread(group=none, target=none, name=none, args=(), kwargs={})this constructor should called keyword arguments. arguments are:
targetcallable object invoked run() method. defaults none, meaning nothing called.
argsargument tuple target invocation. defaults ().
Comments
Post a Comment