简体   繁体   English

Python线程未并行运行

[英]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. 如果我首先启动第一个线程,则命令行将重复打印“ Here”,但从不打印“ Here2”,反之亦然。

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. 编辑:感谢roippi,我能够同时实现这些线程,但是在运行几秒钟后,一个线程似乎占主导地位,而另一个线程不再出现在线程中。 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: 编辑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. 基本上在while循环中,我将条件更改为“ threadStarted”,以确定是否启动线程。 Ontop of this, I ran the module in command line and not IDLE. 在此之上,我在命令行而不是IDLE中运行模块。

That being said, after I incorporated the rest of my code, running in IDLE worked fine. 话虽如此,在我合并了其余代码之后,在IDLE中运行仍然可以正常工作。 Weird. 奇怪的。

threading.Thread(getTouchData(fac))

This is not the proper way to call a Thread . 这不是调用Thread的正确方法。 Thread needs to receive a callable to the target kwarg, and you need to pass args in a tuple to the args kwarg. Thread需要接收可调用target kwarg,并且您需要将元组中的args传递给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. target是要通过run()方法调用的可调用对象。 Defaults to None, meaning nothing is called. 默认为None(无),表示什么都不会被调用。

args is the argument tuple for the target invocation. args是目标调用的参数元组。 Defaults to (). 默认为()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM