python multithreading_操作系统OS,Python - 多进程(multiprocessing)、多线程(multithreading)...
多進程(multiprocessing)
參考:
1. 多進程概念
multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.
例子1. 多進程
from multiprocessing import Process
import os
import time
def info(title):
print(title)
print('module name:', __name__)
print('parent process:', os.getppid())
print('process id:', os.getpid())
print()
def f(name):
info('\033[31;1mfunction f\033[0m')
print('hello', name)
if __name__ == '__main__':
info('\033[32;1mmain process line\033[0m')
p1 = Process(target=f, args=('Allen',))
p1.start()
p2 = Process(target=f, args=('Joyce',))
p2.start()
#把p1,p2 join到caller程序(線程),待p1,p2運行結束后,caller程序(線程)繼續(xù)運行
p1.join()
p2.join()
time.sleep(1)
print()
print('End')
2. 進程間通信看文檔
多線程(multithreading), 留坑
總結
以上是生活随笔為你收集整理的python multithreading_操作系统OS,Python - 多进程(multiprocessing)、多线程(multithreading)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux .net 控制台应用程序,V
- 下一篇: python遍历二维数组_在Python