1、方法
(1)直接创建threading.Thread对象,并把调用对象作为参数传入;
(2)继承threading.Thread类,重写run()方法。
2、实例
import threading import time def catch_fish(): Pass def one_thread(): start_time = time.time() for i in range(1, 1001): catch_fish() end_time = time.time() print("单线程测试 耗时 === %s" % str(end_time - start_time)) def muti_thread(): start_time = time.time() for i in range(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()
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试