0

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.

1
  • If people downvote this question, can they provide an explanation why? I see no reason why this is a bad question. Commented Mar 25, 2014 at 4:12

1 Answer 1

6
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 ().

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, please see the edit; Not fully working for some reason or another.
I'm afraid the issue is external to the code you've posted here. The above code will spawn two threads that loop endlessly. Since I can't run it, I can't help you debug. Try creating a minimal example, then adding in things until it breaks.
Solved it, I think. See above if you're curious.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.