{"id":1585,"date":"2023-03-25T11:33:17","date_gmt":"2023-03-25T03:33:17","guid":{"rendered":""},"modified":"2023-03-25T11:33:17","modified_gmt":"2023-03-25T03:33:17","slug":"Python\u7ebf\u7a0b\u5e76\u53d1","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1585.html","title":{"rendered":"Python\u7ebf\u7a0b\u5e76\u53d1"},"content":{"rendered":"
\n
Python\u7ebf\u7a0b\u5e76\u53d1\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n \u901a\u8fc7\u591a\u5904\u7406\uff0c\u6211\u4eec\u5229\u7528\u4e86\u591a\u4e2a\u8fdb\u7a0b\u7684\u529f\u80fd\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u5229\u7528\u4e86GIL\u7684\u591a\u4e2a\u5b9e\u4f8b\u3002<\/span> Fork<\/span> fork()<\/b>-\u8fd9\u662f\u4e00\u4e2a\u901a\u5e38\u5728\u5185\u6838\u4e2d\u5b9e\u73b0\u7684\u7cfb\u7edf\u8c03\u7528\u3002\u5b83\u7528\u4e8e\u521b\u5efa\u6d41\u7a0b\u7684\u526f\u672c\u3002p> <\/span> \u5bfc\u5165\u591a\u5904\u7406\u6a21\u5757\u3002<\/span> \u4f7f\u7528Forkserver\u673a\u5236\u5b9e\u4f8b\u5316\u670d\u52a1\u5668\u4ee5\u542f\u52a8\u65b0\u8fdb\u7a0b\u3002<\/span> \u6211\u4eec\u9700\u8981\u5b9a\u4e49 Process <\/b>\u7c7b\u7684\u65b0\u5b50\u7c7b\u3002<\/span>\u591a\u5904\u7406<\/h3>\n
\u591a\u7ebf\u7a0b<\/h3>\n
\n\n
\n \u591a\u91cd\u5904\u7406<\/td>\n \u591a\u91cd\u7f16\u7a0b<\/td>\n<\/tr>\n \n \u591a\u5904\u7406\u662f\u6307\u7531\u591a\u4e2aCPU\u540c\u65f6\u5904\u7406\u591a\u4e2a\u8fdb\u7a0b\u3002<\/td>\n \u591a\u91cd\u7f16\u7a0b\u53ef\u5c06\u591a\u4e2a\u7a0b\u5e8f\u540c\u65f6\u4fdd\u5b58\u5728\u4e3b\u5b58\u50a8\u5668\u4e2d\uff0c\u5e76\u4f7f\u7528\u5355\u4e2aCPU\u540c\u65f6\u6267\u884c\u5b83\u4eec\u3002<\/td>\n<\/tr>\n \n \u5b83\u4f7f\u7528\u591a\u4e2aCPU\u3002<\/td>\n \u5b83\u4f7f\u7528\u5355\u4e2aCPU\u3002<\/td>\n<\/tr>\n \n \u5b83\u5141\u8bb8\u5e76\u884c\u5904\u7406\u3002<\/td>\n \u53d1\u751f\u4e0a\u4e0b\u6587\u5207\u6362\u3002<\/td>\n<\/tr>\n \n \u5904\u7406\u5de5\u4f5c\u6240\u9700\u7684\u65f6\u95f4\u66f4\u5c11\u3002<\/td>\n \u66f4\u591a\u7684\u65f6\u95f4\u6765\u5904\u7406\u4f5c\u4e1a\u3002<\/td>\n<\/tr>\n \n \u5b83\u4fc3\u8fdb\u4e86\u8ba1\u7b97\u673a\u7cfb\u7edf\u8bbe\u5907\u7684\u9ad8\u6548\u5229\u7528\u3002<\/td>\n \u6548\u7387\u4f4e\u4e8e\u591a\u91cd\u5904\u7406\u3002<\/td>\n<\/tr>\n \n \u901a\u5e38\u66f4\u8d35\u3002<\/td>\n \u8fd9\u79cd\u7cfb\u7edf\u4fbf\u5b9c\u4e9b\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \u6d88\u9664\u5168\u5c40\u89e3\u91ca\u5668\u9501\u5b9a\uff08GIL\uff09\u7684\u5f71\u54cd<\/h2>\n
\n GIL\uff08\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff09<\/b>\u3002 GIL\u4ece\u672a\u5141\u8bb8\u6211\u4eec\u5229\u7528CPU\u7684\u591a\u4e2a\u5185\u6838\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u8bf4Python\u4e2d\u6ca1\u6709\u771f\u6b63\u7684\u7ebf\u7a0b\u3002 GIL\u662f\u4e92\u65a5\u9501\u2013\u4e92\u65a5\u9501\uff0c\u8fd9\u4f7f\u7ebf\u7a0b\u5b89\u5168\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u8bf4GIL\u963b\u6b62\u4e86\u591a\u4e2a\u7ebf\u7a0b\u5e76\u884c\u6267\u884cPython\u4ee3\u7801\u3002\u4e00\u6b21\u53ea\u80fd\u7531\u4e00\u4e2a\u7ebf\u7a0b\u6301\u6709\u8be5\u9501\uff0c\u5982\u679c\u6211\u4eec\u8981\u6267\u884c\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5219\u5fc5\u987b\u5148\u83b7\u53d6\u8be5\u9501\u3002\n <\/div>\n
\n \u56e0\u6b64\uff0c\u5728\u4efb\u4f55\u65f6\u5019\u90fd\u6ca1\u6709\u9650\u5236\u5728\u6211\u4eec\u7684\u7a0b\u5e8f\u4e2d\u6267\u884c\u4e00\u4e2a\u7ebf\u7a0b\u7684\u5b57\u8282\u7801\u3002<\/span> <\/p>\n\u4f7f\u7528Python\u542f\u52a8\u8fdb\u7a0b<\/h2>\n
\n Spawn<\/span>
\n Forkserver<\/span> <\/p>\n\u4f7f\u7528Fork\u521b\u5efa\u6d41\u7a0b<\/h3>\n
\n getpid()<\/b>-\u6b64\u7cfb\u7edf\u8c03\u7528\u8fd4\u56de\u8c03\u7528\u8fdb\u7a0b\u7684\u8fdb\u7a0bID\uff08PID\uff09\u3002<\/span> <\/p>\n\u793a\u4f8b<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>os
def <\/span>child():
n = os.fork<\/span>()
if <\/span>n > 0:
print(\"PID of Parent process is <\/span>: \"<\/span>, os.getpid<\/span>())
else:<\/span>
print(\"PID of Child process is <\/span>: \"<\/span>, os.getpid<\/span>())
child()
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
PID of Parent process is <\/span>: 25989
PID of Child process is <\/span>: 25990
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u4f7f\u7528Spawn\u521b\u5efa\u6d41\u7a0b<\/h2>\n
\n \u521b\u5efa\u5bf9\u8c61\u8fc7\u7a0b\u3002<\/span>
\n \u901a\u8fc7\u8c03\u7528 start()<\/b>\u65b9\u6cd5\u6765\u542f\u52a8\u6d41\u7a0b\u6d3b\u52a8\u3002<\/span>
\n \u7b49\u5f85\u76f4\u5230\u8fc7\u7a0b\u5b8c\u6210\u5de5\u4f5c\u5e76\u901a\u8fc7\u8c03\u7528 join()<\/b>\u65b9\u6cd5\u9000\u51fa\u3002<\/span> <\/p>\n\u793a\u4f8b<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>multiprocessing
def <\/span>spawn_process(i):
print <\/span>('This <\/span>is <\/span>process: %s'<\/span> %i)
return
if <\/span>__name__ == '__main__'<\/span>:
Process_jobs = []
for <\/span><\/span>i in <\/span>range(3):
p = multiprocessing.Process<\/span>(target = spawn_process, args = (i,))
Process_jobs.append<\/span>(p)
p.start<\/span>()
p.join<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
This <\/span>is <\/span>process: 0
This <\/span>is <\/span>process: 1
This <\/span>is <\/span>process: 2
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u4f7f\u7528Forkserver\u521b\u5efa\u6d41\u7a0b<\/h2>\n
\n \u7136\u540e\u670d\u52a1\u5668\u63a5\u6536\u547d\u4ee4\u5e76\u5904\u7406\u6240\u6709\u521b\u5efa\u65b0\u8fdb\u7a0b\u7684\u8bf7\u6c42\u3002<\/span>
\n \u5bf9\u4e8e\u521b\u5efa\u65b0\u8fdb\u7a0b\uff0c\u6211\u4eec\u7684python\u7a0b\u5e8f\u5c06\u5411Forkserver\u53d1\u9001\u8bf7\u6c42\uff0c\u5e76\u5c06\u4e3a\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u8fdb\u7a0b\u3002<\/span>
\n \u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u65b0\u521b\u5efa\u7684\u8fc7\u7a0b\u3002<\/span> <\/p>\n Python\u4e2d\u7684\u5b88\u62a4\u8fdb\u7a0b<\/h2>\n
\n multiprocessing <\/b>\u6a21\u5757\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u5176daemonic\u9009\u9879\u62e5\u6709\u5b88\u62a4\u8fdb\u7a0b\u3002\u5b88\u62a4\u8fdb\u7a0b\u6216\u5728\u540e\u53f0\u8fd0\u884c\u7684\u8fdb\u7a0b\u9075\u5faa\u4e0e\u5b88\u62a4\u7ebf\u7a0b\u7c7b\u4f3c\u7684\u6982\u5ff5\u3002\u8981\u5728\u540e\u53f0\u6267\u884c\u8be5\u8fc7\u7a0b\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5b88\u62a4\u7a0b\u5e8f\u6807\u5fd7\u8bbe\u7f6e\u4e3atrue\u3002\u53ea\u8981\u4e3b\u8fdb\u7a0b\u6b63\u5728\u6267\u884c\uff0c\u5b88\u62a4\u8fdb\u7a0b\u4fbf\u4f1a\u7ee7\u7eed\u8fd0\u884c\uff0c\u5e76\u5728\u5b8c\u6210\u6267\u884c\u540e\u6216\u7ec8\u6b62\u4e3b\u7a0b\u5e8f\u65f6\u7ec8\u6b62\u3002\n <\/div>\n\u793a\u4f8b<\/h3>\n
\n multithreading <\/b>\u66f4\u6539\u4e3a
\n multiprocessing <\/b>\uff0c\u5e76\u5c06\u5b88\u62a4\u7a0b\u5e8f\u6807\u5fd7\u8bbe\u7f6e\u4e3atrue\u3002\u4f46\u662f\uff0c\u8f93\u51fa\u5c06\u53d1\u751f\u53d8\u5316\uff0c\u5982\u4e0b\u6240\u793a-\n <\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>multiprocessing
import <\/span>time
def <\/span>nondaemonProcess():
print(\"starting my Process\"<\/span>)
time.sleep<\/span>(8)
print(\"ending my Process\"<\/span>)
def <\/span>daemonProcess():
while True<\/span>:
print(\"Hello\"<\/span>)
time.sleep<\/span>(2)
if <\/span>__name__ == '__main__'<\/span>:
nondaemonProcess = multiprocessing.Process<\/span>(target = nondaemonProcess)
daemonProcess = multiprocessing.Process<\/span>(target = daemonProcess)
daemonProcess.daemon = True<\/span>
nondaemonProcess.daemon = False<\/span>
daemonProcess.start<\/span>()
nondaemonProcess.start<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
starting my Process
ending my Process
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u5728Python\u4e2d\u7ec8\u6b62\u8fdb\u7a0b<\/h2>\n
\n terminate()<\/b>\u65b9\u6cd5\u7acb\u5373\u7ec8\u6b62\u6216\u7ec8\u6b62\u8fdb\u7a0b\u3002\u5728\u5b8c\u6210\u6267\u884c\u4e4b\u524d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u6b64\u65b9\u6cd5\u7ec8\u6b62\u5df2\u5728\u51fd\u6570\u7684\u5e2e\u52a9\u4e0b\u521b\u5efa\u7684\u5b50\u8fdb\u7a0b\u3002\n <\/div>\n\u793a\u4f8b<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>multiprocessing
import <\/span>time
def <\/span>Child_process():
print <\/span>('Starting function'<\/span>)
time.sleep<\/span>(5)
print <\/span>('Finished function'<\/span>)
P = multiprocessing.Process<\/span>(target = Child_process)
P.start<\/span>()
print(\"My Process has <\/span>terminated, terminating main <\/span>thread\"<\/span>)
print(\"Terminating Child Process\"<\/span>)
P.terminate<\/span>()
print(\"Child Process successfully terminated\"<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
My Process has <\/span>terminated, terminating main <\/span>thread
Terminating Child Process
Child Process successfully terminated
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8bc6\u522bPython\u4e2d\u7684\u5f53\u524d\u8fdb\u7a0b<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>multiprocessing
print(multiprocessing.current_process<\/span>().pid)
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u793a\u4f8b<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>multiprocessing
import <\/span>time
def <\/span>Child_process():
print(\"PID of Child Process is: {}\"<\/span>.format<\/span>(multiprocessing.current_process<\/span>().pid))
print(\"PID of Main <\/span>process is: {}\"<\/span>.format<\/span>(multiprocessing.current_process<\/span>().pid))
P = multiprocessing.Process<\/span>(target=Child_process)
P.start<\/span>()
P.join<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
PID of Main <\/span>process is: 9401
PID of Child Process is: 9402
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u5728\u5b50\u7c7b\u4e2d\u4f7f\u7528\u8fdb\u7a0b<\/h2>\n
\n threading.Thread <\/b>\u7c7b\u8fdb\u884c\u5b50\u7c7b\u5316\u6765\u521b\u5efa\u7ebf\u7a0b\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5bf9
\n multiprocessing.Process <\/b>\u7c7b\u8fdb\u884c\u5b50\u7c7b\u5316\u6765\u521b\u5efa\u6d41\u7a0b\u3002\u4e3a\u4e86\u5728\u5b50\u7c7b\u4e2d\u4f7f\u7528\u6d41\u7a0b\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9-\n <\/div>\n
\n \u6211\u4eec\u9700\u8981\u91cd\u5199