{"id":1586,"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\u6570\u636e\u4ea4\u4e92","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1586.html","title":{"rendered":"Python\u7ebf\u7a0b\u6570\u636e\u4ea4\u4e92"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python\u7ebf\u7a0b\u6570\u636e\u4ea4\u4e92<\/h1>\n

Python\u7ebf\u7a0b\u6570\u636e\u4ea4\u4e92\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u6d41\u7a0b\u4e92\u901a\u610f\u5473\u7740\u6d41\u7a0b\u4e4b\u95f4\u7684\u6570\u636e\u4ea4\u6362\u3002\u4e3a\u4e86\u5f00\u53d1\u5e76\u884c\u5e94\u7528\u7a0b\u5e8f\uff0c\u5fc5\u987b\u5728\u8fdb\u7a0b\u4e4b\u95f4\u4ea4\u6362\u6570\u636e\u3002\u4e0b\u56fe\u663e\u793a\u4e86\u7528\u4e8e\u591a\u4e2a\u5b50\u8fdb\u7a0b\u4e4b\u95f4\u540c\u6b65\u7684\u5404\u79cd\u901a\u4fe1\u673a\u5236-\n <\/div>\n

Python\u7ebf\u7a0b\u6570\u636e\u4ea4\u4e92_https:\/\/bianchenghao6.com_\u3010Python\u591a\u7ebf\u7a0b\u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\u5404\u79cd\u6c9f\u901a\u673a\u5236<\/h2>\n
\n \u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5404\u79cd\u901a\u4fe1\u673a\u5236\u3002\u673a\u5236\u5982\u4e0b-\n <\/div>\n

\u961f\u5217<\/h3>\n
\n \u961f\u5217\u53ef\u4ee5\u4e0e\u591a\u8fdb\u7a0b\u7a0b\u5e8f\u4e00\u8d77\u4f7f\u7528\u3002
\n multiprocessing <\/b>\u6a21\u5757\u7684Queue\u7c7b\u7c7b\u4f3c\u4e8e
\n Queue.Queue <\/b>\u7c7b\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684API\u3002
\n Multiprocessing <\/b>\u3002Queue\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u5b89\u5168\u7684FIFO\uff08\u5148\u8fdb\u5148\u51fa\uff09\u673a\u5236\uff0c\u7528\u4e8e\u8fdb\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u4ee5\u4e0b\u662f\u4ecepython\u5b98\u65b9\u6587\u6863\u4e2d\u83b7\u53d6\u7684\u6709\u5173\u591a\u5904\u7406\u7684\u7b80\u5355\u793a\u4f8b\uff0c\u4ee5\u4e86\u89e3\u591a\u5904\u7406\u7684Queue\u7c7b\u7684\u6982\u5ff5\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
from <\/span>multiprocessing import <\/span>Process, Queue
import <\/span>queue
import <\/span>random
def <\/span>f(q):
   q.put<\/span>([42, None, 'hello'<\/span>])
def <\/span>main():
   q = Queue()
   p = Process(target = f, args = (q,))
   p.start<\/span>()
   print <\/span>(q.get<\/span>())
if <\/span>__name__ == '__main__'<\/span>:
   main()
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8f93\u51fa<\/h3>\n
\n
 [42, None, 'hello'<\/span>]
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u7ba1\u9053<\/h3>\n
\n \u5b83\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5728\u591a\u8fdb\u7a0b\u7a0b\u5e8f\u4e2d\u7684\u8fdb\u7a0b\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\u3002 Pipe()\u51fd\u6570\u8fd4\u56de\u4e00\u5bf9\u901a\u8fc7\u7ba1\u9053\u8fde\u63a5\u7684\u8fde\u63a5\u5bf9\u8c61\uff0c\u7ba1\u9053\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e3a\u53cc\u5de5\uff08\u53cc\u5411\uff09\u3002\u5b83\u4ee5\u4ee5\u4e0b\u65b9\u5f0f\u5de5\u4f5c-\n <\/div>\n

\u5b83\u8fd4\u56de\u4e00\u5bf9\u4ee3\u8868\u7ba1\u9053\u4e24\u7aef\u7684\u8fde\u63a5\u5bf9\u8c61\u3002<\/span>
\n \u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e24\u4e2a\u65b9\u6cd5\u2013 send()<\/b>\u548c recv()<\/b>\uff0c\u7528\u4e8e\u5728\u8fdb\u7a0b\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\u3002 <\/span> <\/p>\n

\u793a\u4f8b<\/h3>\n
\n \u4ee5\u4e0b\u662f\u4ecepython\u5b98\u65b9\u6587\u6863\u4e2d\u83b7\u53d6\u7684\u6709\u5173\u591a\u5904\u7406\u7684\u7b80\u5355\u793a\u4f8b\uff0c\u4ee5\u4e86\u89e3
\n Pipe()<\/b>\u591a\u5904\u7406\u529f\u80fd\u7684\u6982\u5ff5\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
from <\/span>multiprocessing import <\/span>Process, Pipe
def <\/span>f(conn):
   conn.send<\/span>([42, None, 'hello'<\/span>])
   conn.close<\/span>()
if <\/span>__name__ == '__main__'<\/span>:
   parent_conn, child_conn = Pipe()
   p = Process(target = f, args = (child_conn,))
   p.start<\/span>()
   print <\/span>(parent_conn.recv<\/span>())
   p.join<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8f93\u51fa<\/h3>\n
\n
 [42, None, 'hello'<\/span>]
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Manager<\/h3>\n
\n Manager\u662f\u4e00\u7c7b\u591a\u5904\u7406\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e00\u79cd\u5728\u6240\u6709\u7528\u6237\u4e4b\u95f4\u534f\u8c03\u5171\u4eab\u4fe1\u606f\u7684\u65b9\u6cd5\u3002\u7ba1\u7406\u5668\u5bf9\u8c61\u63a7\u5236\u670d\u52a1\u5668\u8fdb\u7a0b\uff0c\u8be5\u8fdb\u7a0b\u7ba1\u7406\u5171\u4eab\u5bf9\u8c61\u5e76\u5141\u8bb8\u5176\u4ed6\u8fdb\u7a0b\u64cd\u7eb5\u5b83\u4eec\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u7ba1\u7406\u8005\u63d0\u4f9b\u4e86\u4e00\u79cd\u521b\u5efa\u53ef\u4ee5\u5728\u4e0d\u540c\u6d41\u7a0b\u4e4b\u95f4\u5171\u4eab\u7684\u6570\u636e\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u7ba1\u7406\u5668\u5bf9\u8c61\u7684\u4e0d\u540c\u5c5e\u6027-\n <\/div>\n

\u7ba1\u7406\u5668\u7684\u4e3b\u8981\u5c5e\u6027\u662f\u63a7\u5236\u670d\u52a1\u5668\u8fdb\u7a0b\uff0c\u8be5\u8fdb\u7a0b\u7ba1\u7406\u5171\u4eab\u5bf9\u8c61\u3002<\/span>
\n \u53e6\u4e00\u4e2a\u91cd\u8981\u7684\u5c5e\u6027\u662f\u5728\u4efb\u4f55\u8fdb\u7a0b\u4fee\u6539\u5b83\u65f6\u66f4\u65b0\u6240\u6709\u5171\u4eab\u5bf9\u8c61\u3002<\/span> <\/p>\n

\u793a\u4f8b<\/h3>\n
\n \u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u8be5\u793a\u4f8b\u4f7f\u7528manager\u5bf9\u8c61\u5728\u670d\u52a1\u5668\u8fdb\u7a0b\u4e2d\u521b\u5efa\u5217\u8868\u8bb0\u5f55\uff0c\u7136\u540e\u5728\u8be5\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u8bb0\u5f55\u3002\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>print_records(records):
   for <\/span><\/span>record in <\/span>records:
      print(\"Name: {0}\\nScore: {1}\\n\"<\/span>.format<\/span>(record[0], record[1]))
def <\/span>insert_record(record, records):
   records.append<\/span>(record)
      print(\"A New record is <\/span>added\\n\"<\/span>)
if <\/span>__name__ == '__main__'<\/span>:
   with <\/span>multiprocessing.Manager<\/span>() as <\/span>manager:
      records = manager.list<\/span>([('Computers'<\/span>, 1), ('Histoty'<\/span>, 5), ('Hindi'<\/span>,9)])
      new_record = ('English'<\/span>, 3)
      p1 = multiprocessing.Process<\/span>(target = insert_record, args = (new_record, records))
      p2 = multiprocessing.Process<\/span>(target = print_records, args = (records,))
p1.start<\/span>()
      p1.join<\/span>()
      p2.start<\/span>()
      p2.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>
A New record is <\/span>added
Name: Computers
Score: 1
Name: Histoty
Score: 5
Name: Hindi
Score: 9
Name: English
Score: 3
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u7ba1\u7406\u5668\u4e2d\u547d\u540d\u7a7a\u95f4\u7684\u6982\u5ff5<\/h3>\n
\n Manager\u7c7b\u5e26\u6709\u540d\u79f0\u7a7a\u95f4\u7684\u6982\u5ff5\uff0c\u8fd9\u662f\u5728\u591a\u4e2a\u8fdb\u7a0b\u4e4b\u95f4\u5171\u4eab\u591a\u4e2a\u5c5e\u6027\u7684\u5feb\u901f\u65b9\u6cd5\u3002\u547d\u540d\u7a7a\u95f4\u6ca1\u6709\u53ef\u8c03\u7528\u7684\u4efb\u4f55\u516c\u5171\u65b9\u6cd5\uff0c\u4f46\u662f\u5b83\u4eec\u5177\u6709\u53ef\u5199\u7684\u5c5e\u6027\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u4ee5\u4e0bPython\u811a\u672c\u793a\u4f8b\u53ef\u5e2e\u52a9\u6211\u4eec\u5229\u7528\u540d\u79f0\u7a7a\u95f4\u5728\u4e3b\u8fdb\u7a0b\u548c\u5b50\u8fdb\u7a0b\u4e4b\u95f4\u5171\u4eab\u6570\u636e-\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>Mng_NaSp(using_ns):
   using_ns.x +=5
   using_ns.y *= 10
if <\/span>__name__ == '__main__'<\/span>:
   manager = multiprocessing.Manager<\/span>()
   using_ns = manager.Namespace<\/span>()
   using_ns.x = 1
   using_ns.y = 1
   print <\/span>('before'<\/span>, using_ns)
   p = multiprocessing.Process<\/span>(target = Mng_NaSp, args = (using_ns,))
   p.start<\/span>()
   p.join<\/span>()
   print <\/span>('after'<\/span>, using_ns)
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8f93\u51fa<\/h3>\n
\n
 before Namespace(x = 1, y = 1)
after Namespace(x = 6, y = 10)
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Ctypes-Array\u548cValue <\/h2>\n
\n \u591a\u91cd\u5904\u7406\u6a21\u5757\u63d0\u4f9b\u7528\u4e8e\u5c06\u6570\u636e\u5b58\u50a8\u5728\u5171\u4eab\u5185\u5b58\u6620\u5c04\u4e2d\u7684\u6570\u7ec4\u548c\u503c\u5bf9\u8c61\u3002
\n Array <\/b>\u662f\u4ece\u5171\u4eab\u5185\u5b58\u5206\u914d\u7684ctypes\u6570\u7ec4\uff0c\u800c
\n Value <\/b>\u662f\u4ece\u5171\u4eab\u5185\u5b58\u5206\u914d\u7684ctypes\u5bf9\u8c61\u3002\n <\/div>\n
\n \u8981\u4e0e\u4ed6\u4eba\u5728\u4e00\u8d77\uff0c\u8bf7\u4ece\u591a\u5904\u7406\u8fc7\u7a0b\u4e2d\u5bfc\u5165Process\uff0cValue\uff0cArray\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u4ee5\u4e0bPython\u811a\u672c\u662f\u4e00\u4e2a\u53d6\u81eapython\u6587\u6863\u7684\u793a\u4f8b\uff0c\u8be5\u793a\u4f8b\u5229\u7528Ctypes Array\u548cValue\u5728\u8fdb\u7a0b\u4e4b\u95f4\u5171\u4eab\u4e00\u4e9b\u6570\u636e\u3002\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>f(n, a):
   n.value = 3.1415927
   for <\/span><\/span>i in <\/span>range(len(a)):
   a[i] = -a[i]
if <\/span>__name__ == '__main__'<\/span>:
   num = Value('d'<\/span>, 0.0)
   arr = Array('i'<\/span>, range(10))
   p = Process(target = f, args = (num, arr))
   p.start<\/span>()
   p.join<\/span>()
   print <\/span>(num.value)
   print <\/span>(arr[:])
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8f93\u51fa<\/h3>\n
\n
 3.1415927
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u901a\u4fe1\u987a\u5e8f\u8fc7\u7a0b\uff08CSP\uff09<\/h2>\n
\n CSP\u7528\u4e8e\u8bf4\u660e\u7cfb\u7edf\u4e0e\u5176\u4ed6\u5177\u6709\u5e76\u53d1\u6a21\u578b\u7684\u7cfb\u7edf\u4e4b\u95f4\u7684\u4ea4\u4e92\u3002 CSP\u662f\u7528\u4e8e\u901a\u8fc7\u6d88\u606f\u4f20\u9012\u7f16\u5199\u5e76\u53d1\u6216\u7a0b\u5e8f\u7684\u6846\u67b6\uff0c\u56e0\u6b64\u5bf9\u4e8e\u63cf\u8ff0\u5e76\u53d1\u975e\u5e38\u6709\u6548\u3002\n <\/div>\n

Python\u5e93\u2013 PyCSP <\/h2>\n
\n \u4e3a\u4e86\u5b9e\u73b0CSP\u4e2d\u7684\u6838\u5fc3\u539f\u8bed\uff0cPython\u6709\u4e00\u4e2a\u540d\u4e3aPyCSP\u7684\u5e93\u3002\u5b83\u4f7f\u5b9e\u73b0\u975e\u5e38\u7b80\u77ed\u6613\u8bfb\uff0c\u56e0\u6b64\u5f88\u5bb9\u6613\u7406\u89e3\u3002\u4ee5\u4e0b\u662fPyCSP\u7684\u57fa\u672c\u8fc7\u7a0b\u7f51\u7edc-\n <\/div>\n

Python\u7ebf\u7a0b\u6570\u636e\u4ea4\u4e92_https:\/\/bianchenghao6.com_\u3010Python\u591a\u7ebf\u7a0b\u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n

\n \u5728\u4e0a\u8ff0PyCSP\u6d41\u7a0b\u7f51\u7edc\u4e2d\uff0c\u6709\u4e24\u4e2a\u6d41\u7a0b-\u6d41\u7a0b1\u548c\u6d41\u7a0b2\u3002\u8fd9\u4e9b\u6d41\u7a0b\u901a\u8fc7\u5728\u4e24\u4e2a\u901a\u9053\uff08\u901a\u90531\u548c\u901a\u90532\uff09\u4e2d\u4f20\u9012\u6d88\u606f\u8fdb\u884c\u901a\u4fe1\u3002\n <\/div>\n

\u5b89\u88c5PyCSP <\/h3>\n
\n \u501f\u52a9\u4ee5\u4e0b\u547d\u4ee4\uff0c\u6211\u4eec\u53ef\u4ee5\u5b89\u88c5Python\u5e93PyCSP-\n <\/div>\n
\n
 pip install PyCSP
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u4ee5\u4e0bPython\u811a\u672c\u662f\u4e00\u4e2a\u7528\u4e8e\u5e76\u884c\u8fd0\u884c\u4e24\u4e2a\u8fdb\u7a0b\u7684\u7b80\u5355\u793a\u4f8b\u3002\u8fd9\u662f\u501f\u52a9PyCSP python libabary-\n <\/div>\n

\u5b8c\u6210\u7684 <\/p>\n

\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
from <\/span>pycsp.parallel import <\/span>*
import <\/span>time
@process
def <\/span>P1():
   time.sleep<\/span>(1)
   print('P1 exiting'<\/span>)
@process
def <\/span>P2():
   time.sleep<\/span>(1)
   print('P2 exiting'<\/span>)
def <\/span>main():
   Parallel(P1(), P2())
   print('Terminating'<\/span>)
if <\/span>__name__ == '__main__'<\/span>:
   main()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u4e0a\u9762\u7684\u811a\u672c\u4e2d\uff0c\u521b\u5efa\u4e86\u4e24\u4e2a\u529f\u80fd\uff0c\u5373
\n P1 <\/b>\u548c
\n P2 <\/b>\uff0c\u7136\u540e\u7528
\n @process <\/b>\u88c5\u9970\uff0c\u4ee5\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a\u8fdb\u7a0b\u3002\n <\/div>\n

\u8f93\u51fa<\/h3>\n
\n
 P2 exiting
P1 exiting
Terminating
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7ebf\u7a0b\u6570\u636e\u4ea4\u4e92zh-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-1586","post","type-post","status-publish","format-standard","hentry","category-python-dxc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1586"}],"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=1586"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1586\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}