简体   繁体   English

Python创建调用同一函数的不同线程

[英]Python create different thread calling the same function

in my project i have a class of threading.Thread like this: 在我的项目中,我有一个threading.Thread类,如下所示:

class MakeHtml(threading.Thread):
    def __init__(self, *rstext):
        self.outhtml = [x for x in rstext]
        self.retval = ''
        threading.Thread.__init__(self)

    def run(self):
        ...do something

in another file i call, every 10 seconds MakeHtml class 在另一个我调用的文件中,每隔10秒MakeHtml类

t = MakeHtml(mrr1, mrr2, mrr3, mrr4)

for create a thread but in this way i see that the thread is the same every time. 创建线程,但是以此方式我看到线程每次都是相同的。

I need a new thread every time i call the MakeHtml Threading class, how can i do this? 每次调用MakeHtml Threading类时都需要一个新线程,该怎么办?

Thanks in advance 提前致谢

MakeHtml extends Thread, but if you have only 1 instance of MakeHtml, you will have only one thread MakeHtml扩展了Thread,但是如果您只有1个MakeHtml实例,那么您将只有一个线程

For instance if you want 2 different thread you will have to do 例如,如果您想要2个不同的线程,则必须要做

t = MakeHtml(mrr1, mrr2, mrr3, mrr4) # one thread
t1 = MakeHtml(mrr1, mrr2, mrr3, mrr4) # another one

You can use : 您可以使用 :

import threading

def afunction(mm):
   # do job
   pass

threads = []
for mm in [mmr1, mmr2, mmr3n mmr4]:
    t = threading.Thread(target=afunction, args=[mm,])
    threads.append(t)
    t.start()
[t.join() for t in threads]

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

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