{"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
Python\u7ebf\u7a0b\u6570\u636e\u4ea4\u4e92\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n \u5b83\u8fd4\u56de\u4e00\u5bf9\u4ee3\u8868\u7ba1\u9053\u4e24\u7aef\u7684\u8fde\u63a5\u5bf9\u8c61\u3002<\/span> \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> \u5b8c\u6210\u7684 <\/p>\n <\/body> <\/p>\n
\u5404\u79cd\u6c9f\u901a\u673a\u5236<\/h2>\n
\u961f\u5217<\/h3>\n
\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
# 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
[42, None, 'hello'<\/span>]
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u7ba1\u9053<\/h3>\n
\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 Pipe()<\/b>\u591a\u5904\u7406\u529f\u80fd\u7684\u6982\u5ff5\u3002\n <\/div>\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
[42, None, 'hello'<\/span>]
<\/span><\/code><\/pre>\n<\/p><\/div>\nManager<\/h3>\n
\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
# 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
# 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
\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>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
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 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\u793a\u4f8b<\/h3>\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
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
Python\u5e93\u2013 PyCSP <\/h2>\n
<\/p>\n
\u5b89\u88c5PyCSP <\/h3>\n
pip install PyCSP
<\/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>
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 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
P2 exiting
P1 exiting
Terminating
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\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}]}}