不同的线程会在第一个线程完成的同时结束吗? [英] Will different threads end at the same time as the first one to finish?

查看:177
本文介绍了不同的线程会在第一个线程完成的同时结束吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python线程的新手,我有一个问题,假设我启动了如下所示的3个线程,每个线程负责1个不同的任务:

I'm new to thread in python, i have a question that, supposed i start 3 threads like below, each one takes care of 1 different task:

def start( taskName, delay):
   // do somthing with each taskName

# Create two threads as follows
try:
   thread.start_new_thread( start, ("task1", ) )
   thread.start_new_thread( start, ("task2", ) )
   thread.start_new_thread( start, ("task3", ) )
except:
   print "Error: unable to start thread"

假定对于每个开始",大约需要10-15秒才能完成,具体取决于它的每个taskName.我的问题是,如果任务1在12秒内完成,任务2在10秒内完成,任务3在15秒内完成.任务2将完成然后关闭并让任务1和任务3运行直到完成,还是任务2在任务2完成后迫使任务1和3关闭?

Supposed that for each "start", it takes around 10-15 seconds to finish depending on each taskName it is. My question is that, if task 1 finishes in 12 seconds, tasks 2 in 10secs and task 3 in 15 seconds. Will task 2 finish then close and leave task 1 and task 3 to run till finish, or will task 2 force task 1 and 3 to close after task 2 is finished?

是否有任何参数可以传递给start_new_thread方法,以便将上面提到的2件事存档: 1.首先完成迫使其余的关闭. 2.每个单独完成.

Are there any arguments that we can pass to the start_new_thread method in order to archive 2 of the things that I have mentioned above: 1. First to finish forces the rest to close. 2. Each one finish individually.

谢谢

推荐答案

如Max Noel所述,建议使用Thread类而不是start_new_thread.

As Max Noel already mentioned, it is advised to use the Thread class instead of using start_new_thread.

现在,关于您的两个问题:

Now, as for your two questions:

1.首先完成会迫使其他人关闭 您将需要两件重要的事情:一个共享队列,线程完成后就可以将其ID放入其中.还有一个共享事件,该事件将在触发时向所有线程发出信号,通知它们停止工作.主线程将等待第一个线程将某些内容放入队列中,然后触发事件以停止所有线程.

1. First to finish forces the rest to close You will need two important things: a shared queue that the threads can put their ID in once they are done. And a shared Event that will signal all threads to stop working when it is triggered. The main thread will wait for the first thread to put something in the queue and will then trigger the event to stop all threads.

import threading
import random
import time
import Queue

def work(worker_queue, id, stop_event):
    while not stop_event.is_set():
        print "This is worker", id

        # do stuff
        time.sleep(random.random() * 5)

        # put worker ID in queue
        if not stop_event.is_set():
            worker_queue.put(id)

        break

# queue for workers
worker_queue = Queue.Queue()

# indicator for other threads to stop
stop_event = threading.Event()

# run workers
threads = []
threads.append(threading.Thread(target=work, args=(worker_queue, 0, stop_event)))
threads.append(threading.Thread(target=work, args=(worker_queue, 1, stop_event)))
threads.append(threading.Thread(target=work, args=(worker_queue, 2, stop_event)))

for thread in threads:
    thread.start()

# this will block until the first element is in the queue
first_finished = worker_queue.get()

print first_finished, 'was first!'

# signal the rest to stop working
stop_event.set()

2.每个单独完成 现在,这要容易得多.只需在所有Thread对象上调用join方法.这将等待每个线程完成.

2. Each finish individually Now this is much easier. Just call the join method on all Thread objects. This will wait for each thread to finish.

for thread in threads:
    thread.start()

for thread in threads:
    thread.join()

顺便说一句,以上代码适用于Python 2.7.让我知道您是否需要Python 3

Btw, the above code is for Python 2.7. Let me know if you need Python 3

这篇关于不同的线程会在第一个线程完成的同时结束吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆