程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

10、 Python learning notes - thread - multithread demo (IO intensive)

編輯:Python
# Demonstrate multithreading , Explain the multi-threaded pair IO Intensive operation has obvious optimization
"""
1、foo1 loop 2 Time , Every time sleep Time 2 second , After execution is 4 second ,foo2 loop 5 Time , Every time sleep Time 1 second , After execution is 5 second , And it is executed alternately
2、 If it is a single thread, you need 9 More than seconds , Multithreading requires 5 More than seconds , And it is executed in sequence
3、 Only when the cycle is over , The main thread will print ‘ End of main thread ’, explain join Will block the main thread
"""
import threading
import time
def foo1():
for i in range(1, 3):
print('foo1 Function number {} perform '.format(i))
time.sleep(2)
def foo2():
for i in range(1, 6):
print('foo2 Function number {} perform '.format(i))
time.sleep(1)
if __name__ == '__main__':
start_time = time.time()
t1 = threading.Thread(target=foo1)
t2 = threading.Thread(target=foo2)
t1.start()
t2.start()
t1.join()
t2.join()
end_time = start_time - time.time()
print(' Main thread end execution time {}'.format(end_time))

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved