python创建新线程有哪些方法

python创建新线程有哪些方法

1、方法

(1)直接创建threading.Thread对象,并把调用对象作为参数传入;

(2)继承threading.Thread类,重写run()方法。

2、实例

importthreading

importtime

defcatch_fish():

Pass

defone_thread():

start_time=time.time()

foriinrange(1,1001):

catch_fish()

end_time=time.time()

print("单线程测试耗时===%s"%str(end_time-start_time))

defmuti_thread():

start_time=time.time()

foriinrange(1,1001):

threading.Thread(target=catch_fish()).start()

end_time=time.time()

print("多线程测试耗时===%s"%str(end_time-start_time))

if__name__=='__main__':

#单线程

threading.Thread(one_thread()).start()

#多线程

muti_thread()

以上就是python创建新线程的方法,希望对大家有所帮助。更多Python学习教程请关注我们

推荐阅读