在python中创建线程

问题描述:

我有一个脚本,我想要一个函数与另一个函数同时运行。在python中创建线程

示例代码我已经看过:

import threading 


def MyThread (threading.thread): 

    doing something........ 

def MyThread2 (threading.thread): 

    doing something........ 

MyThread().start() 
MyThread2().start() 

我无法得到这个工作。我宁愿使用线程函数而不是类来使用它。

感谢您的任何帮助。

这是工作脚本,感谢所有帮助。

class myClass(): 

    def help(self): 

     os.system('./ssh.py') 

    def nope(self): 
     a = [1,2,3,4,5,6,67,78] 
     for i in a: 
      print i 
      sleep(1) 


if __name__ == "__main__": 
    Yep = myClass() 
    thread = Thread(target = Yep.help) 
    thread2 = Thread(target = Yep.nope) 
    thread.start() 
    thread2.start() 
    thread.join() 
    print 'Finished' 

你并不需要使用的Thread一个子类,使这项工作 - 看看简单的例子我张贴下面来看看如何:

from threading import Thread 
from time import sleep 

def threaded_function(arg): 
    for i in range(arg): 
     print "running" 
     sleep(1) 


if __name__ == "__main__": 
    thread = Thread(target = threaded_function, args = (10,)) 
    thread.start() 
    thread.join() 
    print "thread finished...exiting" 

在这里,我展示了如何使用线程模块创建一个调用普通函数作为其目标的线程。你可以看到我可以在线程构造函数中传递我需要的任何参数。

+0

我试过这个。我在上面添加了脚本。你能告诉我如何获得与第一个函数并行的第二个函数。谢谢 – chrissygormley 2010-05-25 15:35:45

+2

@chrissygormley:join()阻塞,直到第一个线程结束。 – FogleBird 2010-05-25 15:40:53

+3

@chrissygormley:如前所述,加入块直到你加入的线程完成,所以在你的情况下,用第二个函数开始第二个线程作为并行运行两个函数的目标,然后可以选择加入其中的一个如果你只是想等到他们完成。 – jkp 2010-05-25 15:45:32

你重写了run()方法吗?如果您忽略了__init__,您是否确定要拨打基地threading.Thread.__init__()

启动两个线程之后,主线程是否会无限期地继续工作/阻塞/加入子线程,以便主线程执行不会在子线程完成其任务之前结束?

最后,你是否得到任何未处理的异常?

+0

有没有未处理的异常和主线程应运行30分钟。我没有覆盖'__init__'。 run()需要吗?谢谢 – chrissygormley 2010-05-25 15:22:55

+0

我刚刚意识到你的例子是'def MyThread(threading.thread)'...我认为那些是类定义。如果你打算继承threading.thread并用'target = None'初始化线程对象或者省略'target' arg,那么需要run()的实现。否则,如果你只是想在另一个线程中运行一个简单的任务,请参阅jkp的答案。 – 2010-05-25 15:26:51

您可以使用Thread构造函数中的参数target直接传入一个被调用的函数,而不是run

有几个问题与您的代码:

def MyThread (threading.thread): 
  • 你不能用一个函数子类;只有一类
  • 如果你要使用一个子类,你会希望threading.Thread,不threading.thread

如果你真的想只用函数来做到这一点,你有两个选择:

随着线程:

import threading 
def MyThread1(): 
    pass 
def MyThread2(): 
    pass 

t1 = threading.Thread(target=MyThread1, args=[]) 
t2 = threading.Thread(target=MyThread2, args=[]) 
t1.start() 
t2.start() 

螺纹:

import thread 
def MyThread1(): 
    pass 
def MyThread2(): 
    pass 

thread.start_new_thread(MyThread1,()) 
thread.start_new_thread(MyThread2,()) 

Doc for thread.start_new_thread

+2

第二个参数必须是一个**元组**,用于['thread.start_new_thread(function,args [,kwargs])'](https://docs.python.org/2/library/thread.html#thread。 start_new_thread) – venkatvb 2015-06-09 07:00:53

我试着添加另一个join(),它似乎工作。这里是代码

from threading import Thread 
from time import sleep 

def function01(arg,name): 
for i in range(arg): 
    print(name,'i---->',i,'\n') 
    print (name,"arg---->",arg,'\n') 
    sleep(1) 


def test01(): 
    thread1 = Thread(target = function01, args = (10,'thread1',)) 
    thread1.start() 
    thread2 = Thread(target = function01, args = (10,'thread2',)) 
    thread2.start() 
    thread1.join() 
    thread2.join() 
    print ("thread finished...exiting") 



test01()