1、使用最简单的方式
def run_thread():
print "run thread...."
def create_thread():
thread1 = Thread(target=run_thread)
thread1.start()
2、使用类的call方式
from threading import Thread
class thread_func(object):
def __init__(self,func,name,args):
super(thread_func, self).__init__()
self.func = func
self.name = name
self.args = args
def __call__(self):
self.func(*self.args)
def func_def(*args):
for temp in args:
print temp
def main():
thread1 = Thread(target=thread_func(func=func_def,name="test",args=(1,2,3)))
thread1.start()
if __name__ =='__main__':
main()
class daemon_thread(Thread):
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, verbose=None):
super(daemon_thread, self).__init__(group, target, name, args, kwargs, verbose)
self.setDaemon(True)
def run(self):
while True:
time.sleep(3)
print "threading ..."
def create_thread():
thread1 = daemon_thread()
thread1.start()