Python 多线程条件,在第二次迭代时开始线程?

Python 多线程条件,在第二次迭代时开始线程?,python,multithreading,Python,Multithreading,[挑战]: 我有两个线程,第一个是一些信号,第二个是后期处理 对于第一次迭代,我想在一些信号之后运行post_处理 在第一次迭代之后。 我想启动post_处理线程,因此post_处理将使用前一个循环中的一些信号数据 [伪代码]: 第一次迭代: 一些信号 后处理 第二次迭代: 开始踩踏 一些信号和后处理[一些信号-1] [我的尝试]: 我尝试用以下方法实现它,但我不完全确定是否正确: import threading def some_signal(): print thread

[挑战]:

我有两个线程,第一个是一些信号,第二个是后期处理

对于第一次迭代,我想在一些信号之后运行post_处理

在第一次迭代之后。 我想启动post_处理线程,因此post_处理将使用前一个循环中的一些信号数据

[伪代码]:

  • 第一次迭代:
  • 一些信号
  • 后处理
  • 第二次迭代:

  • 开始踩踏

  • 一些信号和后处理[一些信号-1]

[我的尝试]:

我尝试用以下方法实现它,但我不完全确定是否正确:

import threading

def some_signal():
    print threading.currentThread().getName(), 'Get signal'

def post_proccesing():
    print threading.currentThread().getName(), 'Process the signa;'

t = threading.Thread(name='post_proccesing', target=post_proccesing)
w = threading.Thread(name='some_signal', target=some_signal)

flag = 0;
for i in range(5):
    t = threading.Thread(target=some_signal) # use default name

    if flag == 0:
        some_signal() # use default name
        flag  = flag + 1;
    else:
        w = threading.Thread(target=post_proccesing) # use default name
    w.start()
    t.start()

在我看来,您可以基于
i
(您的迭代计数)实现逻辑。也许这样的东西适合你(我不确定你对
标志的意图是什么,所以我删除了它):

输出:

MainThread Get signal
MainThread Process the signa;
Thread-1 Get signal
Thread-2 Process the signa;
Thread-3 Get signal
Thread-4 Process the signa;
Thread-5 Get signal
Thread-6 Process the signa;
Thread-7 Get signal
Thread-8 Process the signa;

阅读旁注:您应该升级到Python3.X,因为2.X将很快停止使用
MainThread Get signal
MainThread Process the signa;
Thread-1 Get signal
Thread-2 Process the signa;
Thread-3 Get signal
Thread-4 Process the signa;
Thread-5 Get signal
Thread-6 Process the signa;
Thread-7 Get signal
Thread-8 Process the signa;