如何在Python中多次调用线程? [英] How can I invoke a thread multiple times in Python?

查看:672
本文介绍了如何在Python中多次调用线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,这是一个愚蠢的问题.我试图使用许多类的多线程来完成不同的工作,这涉及在不同时间多次调用这些多线程.但是我不确定该使用哪种方法.代码如下:

I'm sorry if it is a stupid question. I am trying to use a number of classes of multi-threading to finish different jobs, which involves invoking these multi-threadings at different times for many times. But I am not sure which method to use. The code looks like this:

class workers1(Thread):  
    def __init__(self):  
        Thread.__init__(self)  
    def run(self):  
        do some stuff  

class workers2(Thread):  
    def __init__(self):  
        Thread.__init__(self)  
    def run(self):  
        do some stuff  

class workers3(Thread):  
    def __init__(self):  
        Thread.__init__(self)  
    def run(self):
        do some stuff  

WorkerList1=[workers1(i) for i in range(X)]  
WorkerList2=[workers2(i) for i in range(XX)]  
WorkerList2=[workers3(i) for i in range(XXX)]  


while True:  
    for thread in WorkerList1:  
         thread.run (start? join? or?)
    for thread in WorkerList2:  
          thread.run (start? join? or?)  
    for thread in WorkerList3:  
          thread.run (start? join? or?)  
    do sth  .

我试图让所有WorkerList中的所有线程同时开始或至少在大约同一时间开始运行.在它们全部终止后的某个时间之后,我想再次调用所有线程.

I am trying to have all the threads in all the WorkerList to start functioning at the same time, or at least start around the same time. After sometime once they were all terminated, I would like to invoke all the threads again.

如果没有循环,我可以使用.start;但是由于我只能启动一个线程,因此显然这里不适合启动.如果使用run,则似乎所有线程都按顺序启动,不仅是同一列表中的线程,而且是不同列表中的线程.

If there were no loop, I can just use .start; but since I can only start a thread once, start apparently does not fit here. If I use run, it seems that all the threads start sequentially, not only the threads in the same list, but also threads from different lists.

任何人都可以帮忙吗?

推荐答案

这里有很多误解:

  • 您只能启动一个线程的特定实例一次.但是在您的情况下,for循环遍历线程的不同实例,每个实例在循环中分配给变量thread,因此在每个线程上调用start()方法根本没有问题. (您可以将其视为变量thread是列表中实例化的Thread()对象的别名)

  • you can only start a specific instance of a thread once. but in your case, the for loop is looping over different instances of a thread, each instance being assigned to the variable thread in the loop, so there is no problem at all in calling the start() method over each thread. (you can think of it as if the variable thread is an alias of the Thread() object instantiated in your list)

run()join()是不同的:调用run()的效果就好像您是顺序编程一样. run()方法不会启动新线程,它只是执行该方法中的语句,就像其他任何函数调用一样.

run() is not the same as join(): calling run() performs as if you were programming sequentially. the run() method does not start a new thread, it simply execute the statements in in the method, as for any other function call.

join()不会开始执行任何操作:它仅等待线程完成.为了使join()在线程上正常工作,必须首先在该线程上调用start().

join() does not start executing anything: it only waits for a thread to finish. in order for join() to work properly for a thread, you have to call start() on this thread first.

此外,您应该注意,一旦线程执行完毕,就无法重新启动它:您必须重新创建线程对象才能再次启动它.一种可行的解决方法是在run()方法的末尾调用Thread.__init__().但是,我不建议您这样做,因为这将不允许使用join()方法来检测线程执行的结束.

additionally, you should note that you cannot restart a thread once it has finished execution: you have to recreate the thread object for it to be started again. one workaround to get this working is to call Thread.__init__() at the end of the run() method. however, i would not recommend doing this since this will disallow the use of the join() method to detect the end of execution of the thread.

这篇关于如何在Python中多次调用线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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