#!/usr/bin/env python # coding=utf-8 import time def sleep_3(): time.sleep(3) def sleep_5(): time.sleep(5) if __name__ == '__main__': start_time = time.time() print 'start sleep 3' sleep_3() print 'start sleep 5' sleep_5() end_time = time.time() print str(end_time - start_time) + ' s'
start sleep 3 start sleep 5 8.00100016594 s
#!/usr/bin/env python # coding=utf-8 import time import threading # 引入threading def sleep_3(): time.sleep(3) def sleep_5(): time.sleep(5) if __name__ == '__main__': start_time = time.time() print 'start sleep 3' thread_1 = threading.Thread(target=sleep_3) # 实例化一个线程对象,使线程执行这个函数 thread_1.start() # 启动这个线程 print 'start sleep 5' thread_2 = threading.Thread(target=sleep_5) # 实例化一个线程对象,使线程执行这个函数 thread_2.start() # 启动这个线程 thread_1.join() # 等待thread_1结束 thread_2.join() # 等待thread_2结束 end_time = time.time() print str(end_time - start_time) + ' s'
start sleep 3 start sleep 5 5.00099992752 s
#!/usr/bin/env python # coding=utf-8 import threading def hello_world(): for i in range(100): print 'hello,world' if __name__ == '__main__': my_thread = threading.Thread(target=hello_world) my_thread.start()
#!/usr/bin/env python # coding=utf-8 import threading def hello_world(): for i in range(100): print 'hello,world' if __name__ == '__main__': my_thread = threading.Thread(target=hello_world) my_thread.daemon = True # 设置了标志位True my_thread.start()
#!/usr/bin/env python
# coding=utf-8
import threading
def hello_world(str_1, str_2):
for i in range(10):
print str_1 + str_2
if __name__ == '__main__':
my_thread = threading.Thread(target=hello_world, args=('hello,', 'world')) # 这里传递参数
my_thread.start()
#!/usr/bin/env python # coding=utf-8 import threading class MyThreadHello(threading.Thread): def run(self): for i in range(100): print 'hello' class MyThreadWorld(threading.Thread): def run(self): for i in range(100): print 'world' if __name__ == '__main__': thread_hello = MyThreadHello() thread_world = MyThreadWorld() thread_hello.start() thread_world.start()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有