mHo2
mHo2

Reputation: 103

Python threads not running in parallel

Currently I am trying to have two threads running in parallel, but what seems to be happening is the first thread to be started is the only one that runs.

My code is listed below, however it is more complicated in the end, this is example code that exhibits the exact same behaviour.

If I start the first thread first, the command line will repeatedly print "Here", but never "Here2", and vice versa.

Example output:

Attempt to handshake failed

Attempt to handshake failed
Attempt to handshake failed
here2
here2
here2
here2
here2
here2
here2
here2
here2
here2
here2

and the code:

import serial
import time
import threading
from multiprocessing import Process
from datetime import datetime
from datetime import timedelta
from TableApps import *

#Temporary main function
def main(TTL):
    s = pySerial(TTL);
    count = 0;
    inc = 10;
    x = '';
    fac = None; #changed from '' to singleton "None", it's the coolest way to check for    nothingness -Abe
    while 1:
        if(s.teensyReady == False):
            time.sleep(1);
            print "Attempt to 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: Thanks to roippi I was able to implement these threads simultaneously, however after a few seconds of running, one thread seems to dominate and the other doesn't appear within the thread any longer. In short it would look like

here
here2
here
here2
here
here2
here
here2
here
here
here
here
here
here
here
here
here
here
...

EDIT 2:

Ok, seemed to solve my follow-up issue. Essentially within the while loop i changed my condition to 'threadStarted' to determine whether or not to start the thread. Ontop of this, I ran the module in command line and not IDLE.

That being said, after I incorporated the rest of my code, running in IDLE worked fine. Weird.

Upvotes: 0

Views: 3724

Answers (1)

roippi
roippi

Reputation: 25954

threading.Thread(getTouchData(fac))

This is not the proper way to call a Thread. Thread needs to receive a callable to the target kwarg, and you need to pass args in a tuple to the args kwarg. Altogether:

threading.Thread(target=getTouchData, args=(fac,))

Per the docs:

class threading.Thread(group=None, target=None, name=None, args=(), kwargs={})

This constructor should always be called with keyword arguments. Arguments are:

target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

args is the argument tuple for the target invocation. Defaults to ().

Upvotes: 6

Related Questions