{"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 <\/head>
\n <\/p>\n

\n

Python\u7ebf\u7a0b\u5e76\u53d1<\/h1>\n

Python\u7ebf\u7a0b\u5e76\u53d1\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u653e\u5728\u591a\u5904\u7406\u4e0e\u591a\u7ebf\u7a0b\u4e4b\u95f4\u7684\u6bd4\u8f83\u4e0a\u3002\n <\/div>\n

\u591a\u5904\u7406<\/h3>\n
\n \u5728\u5355\u4e2a\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d\u4f7f\u7528\u4e24\u4e2a\u6216\u591a\u4e2aCPU\u5355\u5143\u3002\u901a\u8fc7\u5229\u7528\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d\u53ef\u7528\u7684CPU\u5185\u6838\u6570\u91cf\uff0c\u8fd9\u662f\u4ece\u786c\u4ef6\u4e2d\u83b7\u5f97\u6700\u5927\u6f5c\u80fd\u7684\u6700\u4f73\u65b9\u6cd5\u3002\n <\/div>\n

\u591a\u7ebf\u7a0b<\/h3>\n
\n \u8fd9\u662fCPU\u901a\u8fc7\u540c\u65f6\u6267\u884c\u591a\u4e2a\u7ebf\u7a0b\u6765\u7ba1\u7406\u64cd\u4f5c\u7cfb\u7edf\u4f7f\u7528\u7684\u80fd\u529b\u3002\u591a\u7ebf\u7a0b\u7684\u4e3b\u8981\u601d\u60f3\u662f\u901a\u8fc7\u5c06\u4e00\u4e2a\u8fdb\u7a0b\u5212\u5206\u4e3a\u591a\u4e2a\u7ebf\u7a0b\u6765\u5b9e\u73b0\u5e76\u884c\u6027\u3002\n <\/div>\n
\n \u4e0b\u8868\u663e\u793a\u4e86\u5b83\u4eec\u4e4b\u95f4\u7684\u4e00\u4e9b\u91cd\u8981\u533a\u522b-\n <\/div>\n\n\n\n\n\n\n\n\n\n
\u591a\u91cd\u5904\u7406<\/td>\n\u591a\u91cd\u7f16\u7a0b<\/td>\n<\/tr>\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
\u5b83\u4f7f\u7528\u591a\u4e2aCPU\u3002<\/td>\n\u5b83\u4f7f\u7528\u5355\u4e2aCPU\u3002<\/td>\n<\/tr>\n
\u5b83\u5141\u8bb8\u5e76\u884c\u5904\u7406\u3002<\/td>\n\u53d1\u751f\u4e0a\u4e0b\u6587\u5207\u6362\u3002<\/td>\n<\/tr>\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
\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
\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 \u5728\u5904\u7406\u5e76\u53d1\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0cPython\u4e2d\u5b58\u5728\u4e00\u4e2a\u9650\u5236\uff0c\u79f0\u4e3a
\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 \u901a\u8fc7\u4f7f\u7528\u591a\u5904\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u7ed5\u8fc7\u7531GIL\u5f15\u8d77\u7684\u9650\u5236-\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>
\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 \u4ee5\u4e0b\u4e09\u79cd\u65b9\u6cd5\u53ef\u7528\u4e8e\u5728\u591a\u5904\u7406\u6a21\u5757\u4e2d\u7684Python\u4e2d\u542f\u52a8\u8fdb\u7a0b-\n <\/div>\n

Fork<\/span>
\n Spawn<\/span>
\n Forkserver<\/span> <\/p>\n

\u4f7f\u7528Fork\u521b\u5efa\u6d41\u7a0b<\/h3>\n
\n Fork\u547d\u4ee4\u662f\u5728UNIX\u4e2d\u627e\u5230\u7684\u6807\u51c6\u547d\u4ee4\u3002\u5b83\u7528\u4e8e\u521b\u5efa\u79f0\u4e3a\u5b50\u6d41\u7a0b\u7684\u65b0\u6d41\u7a0b\u3002\u8be5\u5b50\u8fdb\u7a0b\u4e0e\u79f0\u4e3a\u7236\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u540c\u65f6\u8fd0\u884c\u3002\u8fd9\u4e9b\u5b50\u8fdb\u7a0b\u4e5f\u4e0e\u5176\u7236\u8fdb\u7a0b\u76f8\u540c\uff0c\u5e76\u4e14\u7ee7\u627f\u4e86\u6240\u6709\u53ef\u7528\u4e8e\u7236\u8fdb\u7a0b\u7684\u8d44\u6e90\u3002\u5728\u4f7f\u7528Fork\u521b\u5efa\u8fdb\u7a0b\u65f6\u4f7f\u7528\u4ee5\u4e0b\u7cfb\u7edf\u8c03\u7528-\n <\/div>\n

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>
\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
\n \u4ee5\u4e0bPython\u811a\u672c\u793a\u4f8b\u5c06\u5e2e\u52a9\u60a8\u4e86\u89e3\u5982\u4f55\u521b\u5efa\u65b0\u7684\u5b50\u8fdb\u7a0b\u5e76\u83b7\u53d6\u5b50\u8fdb\u7a0b\u548c\u7236\u8fdb\u7a0b\u7684PID-\n <\/div>\n
\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
\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 Spawn\u610f\u5473\u7740\u5f00\u59cb\u65b0\u7684\u4e8b\u7269\u3002\u56e0\u6b64\uff0c\u4ea7\u751f\u4e00\u4e2a\u8fdb\u7a0b\u610f\u5473\u7740\u7531\u7236\u8fdb\u7a0b\u521b\u5efa\u4e00\u4e2a\u65b0\u8fdb\u7a0b\u3002\u7236\u8fdb\u7a0b\u5f02\u6b65\u7ee7\u7eed\u6267\u884c\uff0c\u6216\u8005\u7b49\u5f85\u76f4\u5230\u5b50\u8fdb\u7a0b\u7ed3\u675f\u6267\u884c\u3002\u8bf7\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\u6765\u751f\u6210\u8fdb\u7a0b-\n <\/div>\n

\u5bfc\u5165\u591a\u5904\u7406\u6a21\u5757\u3002<\/span>
\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
\n \u4ee5\u4e0bPython\u811a\u672c\u793a\u4f8b\u6709\u52a9\u4e8e\u751f\u6210\u4e09\u4e2a\u8fdb\u7a0b\n <\/div>\n
\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
\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 Forkserver\u673a\u5236\u4ec5\u5728\u90a3\u4e9b\u652f\u6301\u901a\u8fc7Unix Pipes\u4f20\u9012\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684UNIX\u5e73\u53f0\u4e0a\u53ef\u7528\u3002\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff0c\u4ee5\u4e86\u89e3Forkserver\u673a\u5236\u7684\u5de5\u4f5c\u539f\u7406-\n <\/div>\n

\u4f7f\u7528Forkserver\u673a\u5236\u5b9e\u4f8b\u5316\u670d\u52a1\u5668\u4ee5\u542f\u52a8\u65b0\u8fdb\u7a0b\u3002<\/span>
\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 Python
\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 \u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528\u4e0e\u5b88\u62a4\u7a0b\u5e8f\u7ebf\u7a0b\u4e2d\u76f8\u540c\u7684\u793a\u4f8b\u3002\u552f\u4e00\u7684\u533a\u522b\u662f\u6a21\u5757\u4ece
\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
\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
\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
\n \u4e0e\u5b88\u62a4\u7a0b\u5e8f\u7ebf\u7a0b\u751f\u6210\u7684\u8f93\u51fa\u76f8\u6bd4\uff0c\u8f93\u51fa\u662f\u4e0d\u540c\u7684\uff0c\u56e0\u4e3a\u6ca1\u6709\u5b88\u62a4\u7a0b\u5e8f\u6a21\u5f0f\u7684\u8fdb\u7a0b\u90fd\u6709\u8f93\u51fa\u3002\u56e0\u6b64\uff0c\u5b88\u62a4\u8fdb\u7a0b\u4f1a\u5728\u4e3b\u7a0b\u5e8f\u7ed3\u675f\u540e\u81ea\u52a8\u7ed3\u675f\uff0c\u4ee5\u907f\u514d\u6301\u7eed\u8fd0\u884c\u8fdb\u7a0b\u3002\n <\/div>\n

\u5728Python\u4e2d\u7ec8\u6b62\u8fdb\u7a0b<\/h2>\n
\n \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528
\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
\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
\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
\n \u8f93\u51fa\u663e\u793a\u7a0b\u5e8f\u5728\u6267\u884c\u901a\u8fc7Child_process()\u51fd\u6570\u521b\u5efa\u7684\u5b50\u8fdb\u7a0b\u4e4b\u524d\u7ec8\u6b62\u3002\u8fd9\u610f\u5473\u7740\u5b50\u8fdb\u7a0b\u5df2\u6210\u529f\u7ec8\u6b62\u3002\n <\/div>\n

\u8bc6\u522bPython\u4e2d\u7684\u5f53\u524d\u8fdb\u7a0b<\/h2>\n
\n \u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u5177\u6709\u79f0\u4e3aPID\u7684\u8fdb\u7a0b\u6807\u8bc6\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u4ee5\u4e0b\u547d\u4ee4\u627e\u51fa\u5f53\u524d\u8fdb\u7a0b\u7684PID-\n <\/div>\n
\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
\n \u4e0b\u9762\u7684Python\u811a\u672c\u793a\u4f8b\u6709\u52a9\u4e8e\u627e\u51fa\u4e3b\u8fdb\u7a0b\u7684PID\u548c\u5b50\u8fdb\u7a0b\u7684PID-\n <\/div>\n
\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
\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 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5bf9
\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

\u6211\u4eec\u9700\u8981\u5b9a\u4e49 Process <\/b>\u7c7b\u7684\u65b0\u5b50\u7c7b\u3002<\/span>
\n \u6211\u4eec\u9700\u8981\u91cd\u5199 _init_\uff08self [\uff0cargs]\uff09<\/b>\u7c7b\u3002<\/span>
\n \u6211\u4eec\u9700\u8981\u91cd\u5199 run\uff08self [\uff0cargs]\uff09<\/b>\u65b9\u6cd5\u7684\uff0c\u4ee5\u5b9e\u73b0 Process <\/b> <\/span>
\n \u6211\u4eec\u9700\u8981\u901a\u8fc7\u8c03\u7528 start()<\/b>\u65b9\u6cd5\u6765\u542f\u52a8\u8be5\u8fc7\u7a0b\u3002<\/span> <\/p>\n

\u793a\u4f8b<\/h3>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>multiprocessing
class <\/span>MyProcess(multiprocessing.Process):
   def <\/span>run(self):
   print <\/span>('called run method in <\/span>process: %s'<\/span> %self.name)
   return
if <\/span>__name__ == '__main__'<\/span>:
   jobs = []
   for <\/span><\/span>i in <\/span>range(5):
   P = MyProcess()
   jobs.append<\/span>(P)
   P.start<\/span>()
   P.join<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8f93\u51fa<\/h3>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
called run method in <\/span>process: MyProcess-1
called run method in <\/span>process: MyProcess-2
called run method in <\/span>process: MyProcess-3
called run method in <\/span>process: MyProcess-4
called run method in <\/span>process: MyProcess-5
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Python\u591a\u5904\u7406\u6a21\u5757\u2013\u6c60\u7c7b<\/h2>\n
\n \u5982\u679c\u6211\u4eec\u5728Python\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8c08\u8bba\u7b80\u5355\u7684\u5e76\u884c
\n \u5904\u7406<\/b>\u4efb\u52a1\uff0c\u90a3\u4e48\u591a\u5904\u7406\u6a21\u5757\u5c06\u4e3a\u6211\u4eec\u63d0\u4f9bPool\u7c7b\u3002
\n Pool <\/b>\u7c7b\u7684\u4ee5\u4e0b\u65b9\u6cd5\u53ef\u7528\u4e8e\u5728\u4e3b\u7a0b\u5e8f\u4e2d\u52a0\u901f\u5b50\u8fdb\u7a0b\u7684\u6570\u91cf\n <\/div>\n

apply()\u65b9\u6cd5<\/h3>\n
\n \u6b64\u65b9\u6cd5\u7c7b\u4f3c\u4e8e
\n .ThreadPoolExecutor\u7684 .submit()<\/b>\u65b9\u6cd5\u3002<\/b>\u5b83\u5c06\u963b\u585e\u76f4\u5230\u7ed3\u679c\u51c6\u5907\u5c31\u7eea\u3002\n <\/div>\n

apply_async()\u65b9\u6cd5<\/h3>\n
\n \u5f53\u6211\u4eec\u9700\u8981\u5e76\u884c\u6267\u884c\u4efb\u52a1\u65f6\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528
\n apply_async()<\/b>\u65b9\u6cd5\u5c06\u4efb\u52a1\u63d0\u4ea4\u5230\u6c60\u4e2d\u3002\u8fd9\u662f\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\uff0c\u53ea\u6709\u5728\u6240\u6709\u5b50\u8fdb\u7a0b\u90fd\u6267\u884c\u5b8c\u4e4b\u540e\uff0c\u624d\u4f1a\u9501\u5b9a\u4e3b\u7ebf\u7a0b\u3002\n <\/div>\n

map()\u65b9\u6cd5<\/h3>\n
\n \u5c31\u50cf
\n apply()<\/b>\u65b9\u6cd5\u4e00\u6837\uff0c\u5b83\u4e5f\u4f1a\u963b\u585e\u76f4\u5230\u7ed3\u679c\u51c6\u5907\u5c31\u7eea\u3002\u5b83\u7b49\u6548\u4e8e\u5185\u7f6e\u7684
\n map()<\/b>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u53ef\u8fed\u4ee3\u7684\u6570\u636e\u5206\u4e3a\u591a\u4e2a\u5757\uff0c\u5e76\u4f5c\u4e3a\u5355\u72ec\u7684\u4efb\u52a1\u63d0\u4ea4\u7ed9\u8fdb\u7a0b\u6c60\u3002\n <\/div>\n

map_async()\u65b9\u6cd5<\/h3>\n
\n \u5b83\u662f
\n map()<\/b>\u65b9\u6cd5\u7684\u4e00\u79cd\u53d8\u4f53\uff0c\u56e0\u4e3a
\n apply_async()<\/b>\u662f
\n apply()<\/b>\u65b9\u6cd5\u7684\u4e00\u90e8\u5206\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\u5bf9\u8c61\u3002\u7ed3\u679c\u51c6\u5907\u5c31\u7eea\u540e\uff0c\u5c06\u5bf9\u5176\u5e94\u7528\u53ef\u8c03\u7528\u5bf9\u8c61\u3002\u53ef\u8c03\u7528\u5bf9\u8c61\u5fc5\u987b\u7acb\u5373\u5b8c\u6210\uff1b\u5426\u5219\uff0c\u5904\u7406\u7ed3\u679c\u7684\u7ebf\u7a0b\u5c06\u88ab\u963b\u585e\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u4ee5\u4e0b\u793a\u4f8b\u5c06\u5e2e\u52a9\u60a8\u5b9e\u73b0\u7528\u4e8e\u6267\u884c\u5e76\u884c\u6267\u884c\u7684\u8fdb\u7a0b\u6c60\u3002\u901a\u8fc7
\n multiprocessing.Pool <\/b>\u65b9\u6cd5\u5e94\u7528
\n square()<\/b>\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u6570\u5b57\u7684\u5e73\u65b9\u8fdb\u884c\u7b80\u5355\u8ba1\u7b97\u3002\u7136\u540e\uff0c\u4f7f\u7528
\n pool.map()<\/b>\u63d0\u4ea45\uff0c\u56e0\u4e3a\u8f93\u5165\u662f\u4e00\u4e2a\u4ece0\u52304\u7684\u6574\u6570\u5217\u8868\u3002\u7ed3\u679c\u5c06\u5b58\u50a8\u5728
\n p_outputs <\/b>\u4e2d\uff0c\u5e76\u4e14\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
def <\/span>square(n):
   result = n*n
   return <\/span>result
if <\/span>__name__ == '__main__'<\/span>:
   inputs = list(range(5))
   p = multiprocessing.Pool<\/span>(processes = 4)
   p_outputs = pool.map<\/span>(function_square, inputs)
   p.close<\/span>()
   p.join<\/span>()
   print <\/span>('Pool :'<\/span>, p_outputs)
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8f93\u51fa<\/h3>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
Pool : [0, 1, 4, 9, 16]
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7ebf\u7a0b\u5e76\u53d1zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[147],"tags":[],"class_list":["post-1585","post","type-post","status-publish","format-standard","hentry","category-python-dxc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1585"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1585"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1585\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}