Python theo dõi lưu lượng mạng

Keras is used by CERN, NASA, NIH, and many more scientific organizations around the world [and yes, Keras is used at the LHC]. Keras has the low-level flexibility to implement arbitrary research ideas while offering optional high-level convenience features to speed up experimentation cycles

The

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 module also introduces APIs which do not have analogs in the
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
0 module. A prime example of this is the
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
5 object which offers a convenient means of parallelizing the execution of a function across multiple input values, distributing the input data across processes [data parallelism]. The following example demonstrates the common practice of defining such functions in a module so that child processes can successfully import that module. This basic example of data parallelism using
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
5,

from multiprocessing import Pool

def f[x]:
    return x*x

if __name__ == '__main__':
    with Pool[5] as p:
        print[p.map[f, [1, 2, 3]]]

will print to standard output

[1, 4, 9]

See also

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
7 offers a higher level interface to push tasks to a background process without blocking execution of the calling process. Compared to using the
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
5 interface directly, the
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
9 API more readily allows the submission of work to the underlying process pool to be separated from waiting for the results

The
from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0 class¶

In

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6, processes are spawned by creating a
from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0 object and then calling its
from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
3 method.
from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0 follows the API of
from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
5. A trivial example of a multiprocess program is

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
5

To show the individual process IDs involved, here is an expanded example

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
6

For an explanation of why the

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
6 part is necessary, see Programming guidelines .

Contexts and start methods¶

Depending on the platform,

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 supports three ways to start a process. These start methods are

spawn

The parent process starts a fresh Python interpreter process. The child process will only inherit those resources necessary to run the process object’s

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
8 method. In particular, unnecessary file descriptors and handles from the parent process will not be inherited. Starting a process using this method is rather slow compared to using fork or forkserver

Available on Unix and Windows. The default on Windows and macOS

fork

The parent process uses

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
9 to fork the Python interpreter. The child process, when it begins, is effectively identical to the parent process. All resources of the parent are inherited by the child process. Note that safely forking a multithreaded process is problematic

Available on Unix only. The default on Unix

forkserver

When the program starts and selects the forkserver start method, a server process is started. From then on, whenever a new process is needed, the parent process connects to the server and requests that it fork a new process. The fork server process is single threaded so it is safe for it to use

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
9. No unnecessary resources are inherited

Available on Unix platforms which support passing file descriptors over Unix pipes

Changed in version 3. 8. On macOS, the spawn start method is now the default. The fork start method should be considered unsafe as it can lead to crashes of the subprocess. See bpo-33725.

Đã thay đổi trong phiên bản 3. 4. spawn added on all Unix platforms, and forkserver added for some Unix platforms. Child processes no longer inherit all of the parents inheritable handles on Windows.

On Unix using the spawn or forkserver start methods will also start a resource tracker process which tracks the unlinked named system resources [such as named semaphores or

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
21 objects] created by processes of the program. When all processes have exited the resource tracker unlinks any remaining tracked object. Usually there should be none, but if a process was killed by a signal there may be some “leaked” resources. [Neither leaked semaphores nor shared memory segments will be automatically unlinked until the next reboot. This is problematic for both objects because the system allows only a limited number of named semaphores, and shared memory segments occupy some space in the main memory. ]

To select a start method you use the

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
22 in the
from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
6 clause of the main module. For example

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
5

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
22 should not be used more than once in the program

Alternatively, you can use

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
25 to obtain a context object. Context objects have the same API as the multiprocessing module, and allow one to use multiple start methods in the same program

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]

Note that objects related to one context may not be compatible with processes for a different context. In particular, locks created using the fork context cannot be passed to processes started using the spawn or forkserver start methods

A library which wants to use a particular start method should probably use

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
25 to avoid interfering with the choice of the library user

Warning

Các phương thức bắt đầu

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
27 và
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
28 hiện không thể được sử dụng với các tệp thực thi "đóng băng" [i. e. , các tệp nhị phân được tạo bởi các gói như PyInstaller và cx_Freeze] trên Unix. Phương thức bắt đầu của
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
29 không hoạt động

Trao đổi đối tượng giữa các tiến trình¶

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 hỗ trợ hai loại kênh liên lạc giữa các quy trình

hàng đợi

Lớp

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
41 gần như là bản sao của lớp
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
42. Ví dụ

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]

Hàng đợi là luồng và xử lý an toàn

ống

Hàm

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
43 trả về một cặp đối tượng kết nối được kết nối bằng một đường ống mà theo mặc định là song công [hai chiều]. Ví dụ

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]

Hai đối tượng kết nối được trả về bởi

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
43 đại diện cho hai đầu của đường ống. Mỗi đối tượng kết nối có các phương thức
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
45 và
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
46 [trong số các phương thức khác]. Lưu ý rằng dữ liệu trong một đường ống có thể bị hỏng nếu hai quy trình [hoặc luồng] cố gắng đọc hoặc ghi vào cùng một đầu của đường ống cùng một lúc. Tất nhiên, không có nguy cơ tham nhũng từ các quy trình sử dụng các đầu khác nhau của đường ống cùng một lúc

Đồng bộ hóa giữa các quy trình¶

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 chứa các giá trị tương đương của tất cả các nguyên mẫu đồng bộ hóa từ
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
0. Chẳng hạn, người ta có thể sử dụng khóa để đảm bảo rằng mỗi lần chỉ có một quy trình in ra đầu ra tiêu chuẩn

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
2

Không sử dụng đầu ra khóa từ các quy trình khác nhau có thể bị lẫn lộn

Chia sẻ trạng thái giữa các tiến trình¶

Như đã đề cập ở trên, khi thực hiện lập trình đồng thời, tốt nhất là tránh sử dụng trạng thái chia sẻ càng nhiều càng tốt. Điều này đặc biệt đúng khi sử dụng nhiều quy trình

Tuy nhiên, nếu bạn thực sự cần sử dụng một số dữ liệu được chia sẻ thì

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 sẽ cung cấp một số cách để thực hiện việc đó

Bộ nhớ dùng chung

Dữ liệu có thể được lưu trữ trong bản đồ bộ nhớ dùng chung bằng cách sử dụng

[1, 4, 9]
00 hoặc
[1, 4, 9]
01. Ví dụ, đoạn mã sau

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
4

sẽ in

[1, 4, 9]
0

Các đối số

[1, 4, 9]
02 và
[1, 4, 9]
03 được sử dụng khi tạo
[1, 4, 9]
04 và
[1, 4, 9]
05 là các loại mã được sử dụng bởi mô-đun
[1, 4, 9]
06.
[1, 4, 9]
02 biểu thị số float có độ chính xác kép và
[1, 4, 9]
03 biểu thị số nguyên đã ký. Các đối tượng được chia sẻ này sẽ được xử lý và an toàn cho luồng

Để linh hoạt hơn trong việc sử dụng bộ nhớ dùng chung, người ta có thể sử dụng mô-đun

[1, 4, 9]
09 hỗ trợ tạo các đối tượng ctypes tùy ý được cấp phát từ bộ nhớ dùng chung

quy trình máy chủ

Một đối tượng người quản lý được trả về bởi

[1, 4, 9]
10 điều khiển một quy trình máy chủ chứa các đối tượng Python và cho phép các quy trình khác thao tác chúng bằng proxy

Người quản lý do

[1, 4, 9]
10 trả lại sẽ hỗ trợ các loại
[1, 4, 9]
12,
[1, 4, 9]
13,
[1, 4, 9]
14,
[1, 4, 9]
15,
[1, 4, 9]
16,
[1, 4, 9]
17,
[1, 4, 9]
18,
[1, 4, 9]
19,
[1, 4, 9]
20,
[1, 4, 9]
21,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
41,
[1, 4, 9]
00 và
[1, 4, 9]
01. Ví dụ,

[1, 4, 9]
1

sẽ in

[1, 4, 9]
2

Server process managers are more flexible than using shared memory objects because they can be made to support arbitrary object types. Also, a single manager can be shared by processes on different computers over a network. They are, however, slower than using shared memory

Using a pool of workers¶

The

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
5 class represents a pool of worker processes. It has methods which allows tasks to be offloaded to the worker processes in a few different ways

Ví dụ

[1, 4, 9]
3

Note that the methods of a pool should only ever be used by the process which created it

Note

Functionality within this package requires that the

[1, 4, 9]
26 module be importable by the children. This is covered in Programming guidelines however it is worth pointing out here. This means that some examples, such as the
[1, 4, 9]
27 examples will not work in the interactive interpreter. For example.

[1, 4, 9]
4

[If you try this it will actually output three full tracebacks interleaved in a semi-random fashion, and then you may have to stop the parent process somehow. ]

Reference¶

The

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 package mostly replicates the API of the
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
0 module

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0 and exceptions¶

class multiprocessing. Process[group=None , target=None , name=None , args=[] , kwargs={} , * , daemon=None]

Process objects represent activity that is run in a separate process. The

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0 class has equivalents of all the methods of
from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
5

The constructor should always be called with keyword arguments. group should always be

[1, 4, 9]
33; it exists solely for compatibility with
from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
5. target is the callable object to be invoked by the
from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
8 method. It defaults to
[1, 4, 9]
33, meaning nothing is called. name is the process name [see
[1, 4, 9]
37 for more details]. args is the argument tuple for the target invocation. kwargs is a dictionary of keyword arguments for the target invocation. If provided, the keyword-only daemon argument sets the process
[1, 4, 9]
38 flag to
[1, 4, 9]
39 or
[1, 4, 9]
40. If
[1, 4, 9]
33 [the default], this flag will be inherited from the creating process

By default, no arguments are passed to target. The args argument, which defaults to

[1, 4, 9]
42, can be used to specify a list or tuple of the arguments to pass to target

If a subclass overrides the constructor, it must make sure it invokes the base class constructor [

[1, 4, 9]
43] before doing anything else to the process

Changed in version 3. 3. Added the daemon argument.

run[]

Method representing the process’s activity

You may override this method in a subclass. The standard

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
8 method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively

Using a list or tuple as the args argument passed to

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0 achieves the same effect

Example

[1, 4, 9]
5

start[]

Bắt đầu hoạt động của quy trình

Điều này phải được gọi nhiều nhất một lần cho mỗi đối tượng quy trình. Nó sắp xếp để gọi phương thức

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
8 của đối tượng trong một quy trình riêng biệt

join[[timeout]]

Nếu thời gian chờ của đối số tùy chọn là

[1, 4, 9]
33 [mặc định], phương thức sẽ chặn cho đến khi quá trình có phương thức
[1, 4, 9]
48 được gọi kết thúc. Nếu thời gian chờ là một số dương, nó sẽ chặn tối đa các giây hết thời gian chờ. Lưu ý rằng phương thức trả về
[1, 4, 9]
33 nếu quá trình của nó kết thúc hoặc nếu phương thức hết thời gian. Kiểm tra
[1, 4, 9]
50 của quy trình để xác định xem nó có bị chấm dứt hay không

Một quá trình có thể được tham gia nhiều lần

A process cannot join itself because this would cause a deadlock. Có lỗi khi cố gắng tham gia một quy trình trước khi nó được bắt đầu

tên

Tên quy trình. Tên là một chuỗi chỉ được sử dụng cho mục đích nhận dạng. Nó không có ngữ nghĩa. Nhiều quá trình có thể được đặt tên giống nhau

Tên ban đầu được đặt bởi hàm tạo. Nếu không có tên rõ ràng nào được cung cấp cho hàm tạo, tên có dạng 'Process-N1. N2. …. Nk' được xây dựng, trong đó mỗi Nk là con thứ N của cha mẹ của nó

is_alive[]

Trả về xem quá trình có còn hoạt động không

Đại khái, một đối tượng tiến trình vẫn còn hoạt động kể từ thời điểm phương thức

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
3 trả về cho đến khi tiến trình con kết thúc

daemon

Cờ daemon của tiến trình, một giá trị Boolean. Điều này phải được đặt trước khi

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
3 được gọi

Giá trị ban đầu được kế thừa từ quá trình tạo

Khi một tiến trình thoát, nó sẽ cố gắng chấm dứt tất cả các tiến trình con daemon của nó

Lưu ý rằng quy trình daemon không được phép tạo quy trình con. Mặt khác, một quy trình daemon sẽ khiến các con của nó mồ côi nếu nó bị chấm dứt khi quá trình cha mẹ của nó thoát ra. Ngoài ra, đây không phải là dịch vụ hoặc daemon Unix, chúng là các quy trình bình thường sẽ bị chấm dứt [và không được tham gia] nếu các quy trình không phải daemon đã thoát

In addition to the

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
5 API,
from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0 objects also support the following attributes and methods

pid

Return the process ID. Before the process is spawned, this will be

[1, 4, 9]
33

exitcode

The child’s exit code. This will be

[1, 4, 9]
33 if the process has not yet terminated

If the child’s

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
8 method returned normally, the exit code will be 0. If it terminated via
[1, 4, 9]
58 with an integer argument N, the exit code will be N

If the child terminated due to an exception not caught within

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
8, the exit code will be 1. If it was terminated by signal N, the exit code will be the negative value -N

authkey

The process’s authentication key [a byte string]

When

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 is initialized the main process is assigned a random string using
[1, 4, 9]
61

When a

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0 object is created, it will inherit the authentication key of its parent process, although this may be changed by setting
[1, 4, 9]
63 to another byte string

See Authentication keys .

sentinel

A numeric handle of a system object which will become “ready” when the process ends

You can use this value if you want to wait on several events at once using

[1, 4, 9]
64. Otherwise calling
[1, 4, 9]
48 is simpler

On Windows, this is an OS handle usable with the

[1, 4, 9]
66 and
[1, 4, 9]
67 family of API calls. On Unix, this is a file descriptor usable with primitives from the
[1, 4, 9]
68 module

New in version 3. 3

terminate[]

Terminate the process. On Unix this is done using the

[1, 4, 9]
69 signal; on Windows
[1, 4, 9]
70 is used. Note that exit handlers and finally clauses, etc. , will not be executed

Note that descendant processes of the process will not be terminated – they will simply become orphaned

Warning

If this method is used when the associated process is using a pipe or queue then the pipe or queue is liable to become corrupted and may become unusable by other process. Similarly, if the process has acquired a lock or semaphore etc. then terminating it is liable to cause other processes to deadlock

kill[]

Tương tự như

[1, 4, 9]
71 nhưng sử dụng tín hiệu
[1, 4, 9]
72 trên Unix

New in version 3. 7

close[]

Đóng đối tượng

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0, giải phóng tất cả các tài nguyên được liên kết với nó.
[1, 4, 9]
74 is raised if the underlying process is still running. Once
[1, 4, 9]
75 returns successfully, most other methods and attributes of the
from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0 object will raise
[1, 4, 9]
74

New in version 3. 7

Note that the

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
3,
[1, 4, 9]
48,
[1, 4, 9]
80,
[1, 4, 9]
71 and
[1, 4, 9]
50 methods should only be called by the process that created the process object

Example usage of some of the methods of

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0

[1, 4, 9]
6

exception multiprocessing. ProcessError

The base class of all

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 exceptions

exception multiprocessing. BufferTooShort

Exception raised by

[1, 4, 9]
85 when the supplied buffer object is too small for the message read

If

[1, 4, 9]
86 is an instance of
[1, 4, 9]
87 then
[1, 4, 9]
88 will give the message as a byte string

exception multiprocessing. AuthenticationError

Raised when there is an authentication error

exception multiprocessing. TimeoutError

Raised by methods with a timeout when the timeout expires

Pipes and Queues¶

When using multiple processes, one generally uses message passing for communication between processes and avoids having to use any synchronization primitives like locks

For passing messages one can use

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
43 [for a connection between two processes] or a queue [which allows multiple producers and consumers]

The

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
41,
[1, 4, 9]
91 and
[1, 4, 9]
92 types are multi-producer, multi-consumer FIFO queues modelled on the
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
42 class in the standard library. They differ in that
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
41 lacks the
[1, 4, 9]
95 and
[1, 4, 9]
48 methods introduced into Python 2. 5’s
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
42 class

If you use

[1, 4, 9]
92 then you must call
[1, 4, 9]
99 for each task removed from the queue or else the semaphore used to count the number of unfinished tasks may eventually overflow, raising an exception

Note that one can also create a shared queue by using a manager object – see Managers .

Note

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 uses the usual
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
501 and
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
502 exceptions to signal a timeout. They are not available in the
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 namespace so you need to import them from
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
504

Note

When an object is put on a queue, the object is pickled and a background thread later flushes the pickled data to an underlying pipe. This has some consequences which are a little surprising, but should not cause any practical difficulties – if they really bother you then you can instead use a queue created with a manager .

  1. After putting an object on an empty queue there may be an infinitesimal delay before the queue’s

    from multiprocessing import Process, Queue
    
    def f[q]:
        q.put[[42, None, 'hello']]
    
    if __name__ == '__main__':
        q = Queue[]
        p = Process[target=f, args=[q,]]
        p.start[]
        print[q.get[]]    # prints "[42, None, 'hello']"
        p.join[]
    
    505 method returns
    [1, 4, 9]
    
    40 and
    from multiprocessing import Process, Queue
    
    def f[q]:
        q.put[[42, None, 'hello']]
    
    if __name__ == '__main__':
        q = Queue[]
        p = Process[target=f, args=[q,]]
        p.start[]
        print[q.get[]]    # prints "[42, None, 'hello']"
        p.join[]
    
    507 can return without raising
    from multiprocessing import Process, Queue
    
    def f[q]:
        q.put[[42, None, 'hello']]
    
    if __name__ == '__main__':
        q = Queue[]
        p = Process[target=f, args=[q,]]
        p.start[]
        print[q.get[]]    # prints "[42, None, 'hello']"
        p.join[]
    
    501

  2. If multiple processes are enqueuing objects, it is possible for the objects to be received at the other end out-of-order. However, objects enqueued by the same process will always be in the expected order with respect to each other

Warning

If a process is killed using

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
509 or
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
510 while it is trying to use a
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
41, then the data in the queue is likely to become corrupted. This may cause any other process to get an exception when it tries to use the queue later on

Warning

As mentioned above, if a child process has put items on a queue [and it has not used

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
512], then that process will not terminate until all buffered items have been flushed to the pipe

This means that if you try joining that process you may get a deadlock unless you are sure that all items which have been put on the queue have been consumed. Similarly, if the child process is non-daemonic then the parent process may hang on exit when it tries to join all its non-daemonic children

Note that a queue created using a manager does not have this issue. See Programming guidelines .

For an example of the usage of queues for interprocess communication see Examples .

multiprocessing. Pipe[[duplex]]

Returns a pair

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
513 of
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
514 objects representing the ends of a pipe

If duplex is

[1, 4, 9]
39 [the default] then the pipe is bidirectional. If duplex is
[1, 4, 9]
40 then the pipe is unidirectional.
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
517 can only be used for receiving messages and
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
518 can only be used for sending messages

class multiprocessing. Queue[[maxsize]]

Returns a process shared queue implemented using a pipe and a few locks/semaphores. When a process first puts an item on the queue a feeder thread is started which transfers objects from a buffer into the pipe

The usual

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
501 and
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
502 exceptions from the standard library’s
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
504 module are raised to signal timeouts

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
41 implements all the methods of
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
42 except for
[1, 4, 9]
95 and
[1, 4, 9]
48

qsize[]

Return the approximate size of the queue. Because of multithreading/multiprocessing semantics, this number is not reliable

Note that this may raise

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
526 on Unix platforms like macOS where
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
527 is not implemented

empty[]

Return

[1, 4, 9]
39 if the queue is empty,
[1, 4, 9]
40 otherwise. Because of multithreading/multiprocessing semantics, this is not reliable

full[]

Trả lại

[1, 4, 9]
39 nếu hàng đợi đã đầy, ngược lại là
[1, 4, 9]
40. Because of multithreading/multiprocessing semantics, this is not reliable

đặt[obj[ , block[, timeout]]]

Put obj into the queue. If the optional argument block is

[1, 4, 9]
39 [the default] and timeout is
[1, 4, 9]
33 [the default], block if necessary until a free slot is available. Nếu thời gian chờ là một số dương, nó sẽ chặn hầu hết các giây hết thời gian chờ và tăng ngoại lệ
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
502 nếu không có chỗ trống trong thời gian đó. Mặt khác [khối là
[1, 4, 9]
40], hãy đặt một mục vào hàng đợi nếu có sẵn một vị trí trống ngay lập tức, nếu không thì tăng ngoại lệ
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
502 [thời gian chờ bị bỏ qua trong trường hợp đó]

Đã thay đổi trong phiên bản 3. 8. Nếu hàng đợi đã đóng,

[1, 4, 9]
74 được nâng lên thay vì
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
538.

put_nowait[obj]

Tương đương với

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
539

get[[block[ , timeout]]]

Xóa và trả lại một mục khỏi hàng đợi. Nếu khối đối số tùy chọn là

[1, 4, 9]
39 [mặc định] và thời gian chờ là
[1, 4, 9]
33 [mặc định], hãy chặn nếu cần cho đến khi có sẵn một mục. Nếu thời gian chờ là một số dương, nó sẽ chặn tối đa các giây hết thời gian chờ và tăng ngoại lệ
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
501 nếu không có mục nào có sẵn trong thời gian đó. Otherwise [block is
[1, 4, 9]
40], return an item if one is immediately available, else raise the
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
501 exception [timeout is ignored in that case]

Đã thay đổi trong phiên bản 3. 8. If the queue is closed,

[1, 4, 9]
74 is raised instead of
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
546.

get_nowait[]

Equivalent to

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
547

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
548 có một số phương thức bổ sung không có trong
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
42. Các phương thức này thường không cần thiết đối với hầu hết mã

close[]

Cho biết rằng quy trình hiện tại sẽ không đưa thêm dữ liệu vào hàng đợi này. Chủ đề nền sẽ thoát sau khi nó đã xóa tất cả dữ liệu được lưu vào bộ đệm vào đường ống. Điều này được gọi tự động khi hàng đợi được thu gom rác

join_thread[]

Tham gia chủ đề nền. Điều này chỉ có thể được sử dụng sau khi

[1, 4, 9]
75 đã được gọi. Nó chặn cho đến khi luồng nền thoát ra, đảm bảo rằng tất cả dữ liệu trong bộ đệm đã được chuyển sang đường ống

Theo mặc định, nếu một quy trình không phải là người tạo hàng đợi thì khi thoát, nó sẽ cố gắng tham gia luồng nền của hàng đợi. Quá trình có thể gọi ________ 1551 để khiến ________ 1552 không làm gì cả

cancel_join_thread[]

Prevent

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
552 from blocking. In particular, this prevents the background thread from being joined automatically when the process exits – see
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
552

Tên tốt hơn cho phương pháp này có thể là

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
555. Nó có khả năng làm mất dữ liệu trong hàng đợi và bạn gần như chắc chắn sẽ không cần sử dụng nó. Nó thực sự chỉ ở đó nếu bạn cần quy trình hiện tại thoát ngay lập tức mà không cần chờ xóa dữ liệu đã xử lý vào đường ống bên dưới và bạn không quan tâm đến dữ liệu bị mất

Note

Chức năng của lớp này yêu cầu triển khai semaphore được chia sẻ chức năng trên hệ điều hành máy chủ. Nếu không có một, chức năng trong lớp này sẽ bị vô hiệu hóa và cố gắng khởi tạo một

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
41 sẽ dẫn đến một
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
557. See bpo-3770 for additional information. The same holds true for any of the specialized queue types listed below

lớp đa xử lý. Queue đơn giản

Nó là loại

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
41 được đơn giản hóa, rất gần với loại
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
559 bị khóa

close[]

Close the queue. release internal resources

Một hàng đợi không được sử dụng nữa sau khi nó bị đóng. Ví dụ: các phương thức

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
560,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
561 và
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
505 không còn được gọi nữa

Mới trong phiên bản 3. 9

empty[]

Trả lại

[1, 4, 9]
39 nếu hàng đợi trống, ngược lại là
[1, 4, 9]
40

get[]

Xóa và trả lại một mục khỏi hàng đợi

put[item]

Đặt mục vào hàng đợi

lớp đa xử lý. JoinableQueue[[maxsize]]

[1, 4, 9]
92, một lớp con của
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
41, là một hàng đợi có thêm các phương thức
[1, 4, 9]
95 và
[1, 4, 9]
48

task_done[]

Chỉ ra rằng một nhiệm vụ được xử lý trước đây đã hoàn thành. Used by queue consumers. Đối với mỗi

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
560 được sử dụng để tìm nạp một tác vụ, một cuộc gọi tiếp theo tới
[1, 4, 9]
95 sẽ báo cho hàng đợi rằng quá trình xử lý tác vụ đã hoàn tất

Nếu một

[1, 4, 9]
48 hiện đang bị chặn, nó sẽ tiếp tục khi tất cả các mục đã được xử lý [có nghĩa là đã nhận được cuộc gọi
[1, 4, 9]
95 cho mọi mục đã được
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
561 vào hàng đợi]

Raises a

[1, 4, 9]
74 if called more times than there were items placed in the queue

join[]

Chặn cho đến khi tất cả các mục trong hàng đợi đã được nhận và xử lý

Số lượng nhiệm vụ chưa hoàn thành tăng lên bất cứ khi nào một mục được thêm vào hàng đợi. Số lượng giảm xuống bất cứ khi nào người tiêu dùng gọi

[1, 4, 9]
95 để cho biết rằng mặt hàng đã được lấy và mọi công việc trên mặt hàng đó đã hoàn tất. Khi số nhiệm vụ chưa hoàn thành giảm xuống 0,
[1, 4, 9]
48 sẽ bỏ chặn

Miscellaneous¶

multiprocessing. active_children[]

Trả về danh sách tất cả các phần tử con còn sống của tiến trình hiện tại

Gọi điều này có tác dụng phụ là “tham gia” bất kỳ quy trình nào đã kết thúc

multiprocessing. cpu_count[]

Trả về số lượng CPU trong hệ thống

Con số này không tương đương với số lượng CPU mà tiến trình hiện tại có thể sử dụng. Số lượng CPU có thể sử dụng có thể thu được với

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
577

When the number of CPUs cannot be determined a

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
526 is raised

See also

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
579

đa xử lý. current_ process[]

Trả về đối tượng

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0 tương ứng với quy trình hiện tại

An analogue of

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
581

multiprocessing. parent_process[]

Return the

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0 object corresponding to the parent process of the
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
583. For the main process,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
584 will be
[1, 4, 9]
33

New in version 3. 8

multiprocessing. freeze_support[]

Thêm hỗ trợ khi chương trình sử dụng

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 bị đóng băng để tạo tệp thực thi Windows. [Has been tested with py2exe, PyInstaller and cx_Freeze. ]

One needs to call this function straight after the

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
6 line of the main module. Ví dụ

[1, 4, 9]
7

Nếu dòng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
588 bị bỏ qua thì việc cố chạy tệp thực thi bị đóng băng sẽ tăng
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
589

Calling

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
588 has no effect when invoked on any operating system other than Windows. Ngoài ra, nếu mô-đun đang được trình thông dịch Python trên Windows chạy bình thường [chương trình chưa bị đóng băng] thì
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
588 không có hiệu lực

multiprocessing. get_all_start_methods[]

Trả về danh sách các phương thức bắt đầu được hỗ trợ, phương thức đầu tiên là mặc định. Các phương pháp bắt đầu có thể là

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
29,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
27 và
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
28. On Windows only
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
27 is available. On Unix
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
29 and
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
27 are always supported, with
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
29 being the default

Mới trong phiên bản 3. 4

đa xử lý. get_context[phương thức=Không]

Trả về một đối tượng ngữ cảnh có cùng thuộc tính với mô-đun

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6

Nếu phương thức là

[1, 4, 9]
33 thì ngữ cảnh mặc định được trả về. Mặt khác, phương thức phải là
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
29,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
27,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
28.
[1, 4, 9]
74 is raised if the specified start method is not available

Mới trong phiên bản 3. 4

multiprocessing. get_start_method[allow_none=False]

Return the name of start method used for starting processes

If the start method has not been fixed and allow_none is false, then the start method is fixed to the default and the name is returned. If the start method has not been fixed and allow_none is true then

[1, 4, 9]
33 is returned

The return value can be

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
29,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
27,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
28 or
[1, 4, 9]
33.
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
29 là mặc định trên Unix, trong khi
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
27 là mặc định trên Windows và macOS

Changed in version 3. 8. On macOS, the spawn start method is now the default. The fork start method should be considered unsafe as it can lead to crashes of the subprocess. See bpo-33725.

Mới trong phiên bản 3. 4

multiprocessing. set_executable[có thể thực thi]

Set the path of the Python interpreter to use when starting a child process. [Theo mặc định,

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
612 được sử dụng]. Embedders có thể sẽ cần phải làm một cái gì đó như

[1, 4, 9]
8

trước khi họ có thể tạo các tiến trình con

Đã thay đổi trong phiên bản 3. 4. Hiện được hỗ trợ trên Unix khi sử dụng phương thức khởi động

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
27.

Đã thay đổi trong phiên bản 3. 11. Chấp nhận một đối tượng giống đường dẫn .

đa xử lý. set_start_method[phương thức , lực lượng=False]

Set the method which should be used to start child processes. The method argument can be

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
29,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
27 or
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
28. Tăng
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
589 nếu phương thức bắt đầu đã được đặt và lực không phải là
[1, 4, 9]
39. Nếu phương thức là
[1, 4, 9]
33 và lực lượng là
[1, 4, 9]
39 thì phương thức bắt đầu được đặt thành
[1, 4, 9]
33. Nếu phương thức là
[1, 4, 9]
33 và lực lượng là
[1, 4, 9]
40 thì bối cảnh được đặt thành bối cảnh mặc định

Note that this should be called at most once, and it should be protected inside the

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
6 clause of the main module

Mới trong phiên bản 3. 4

Note

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 không chứa từ tương tự của
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
626,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
627,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
628,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
629,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
630 hoặc
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
631

Đối tượng kết nối¶

Các đối tượng kết nối cho phép gửi và nhận các đối tượng hoặc chuỗi có thể chọn. They can be thought of as message oriented connected sockets

Connection objects are usually created using

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
559 – see also Listeners and Clients .

lớp đa xử lý. sự liên quan. Kết nốigửi[obj]

Gửi một đối tượng đến đầu kia của kết nối sẽ được đọc bằng cách sử dụng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
46

Đối tượng phải được picklable. Dưa chua rất lớn [khoảng 32 MiB+, mặc dù nó phụ thuộc vào HĐH] có thể gây ra ngoại lệ

[1, 4, 9]
74

recv[]

Trả lại một đối tượng được gửi từ đầu kia của kết nối bằng cách sử dụng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
45. Chặn cho đến khi có thứ gì đó để nhận. Tăng
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
636 nếu không còn gì để nhận và đầu kia đã đóng

fileno[]

Trả lại bộ mô tả tệp hoặc tay cầm được sử dụng bởi kết nối

close[]

Đóng kết nối

This is called automatically when the connection is garbage collected

poll[[timeout]]

Trả về xem có bất kỳ dữ liệu nào có sẵn để đọc không

Nếu thời gian chờ không được chỉ định thì nó sẽ quay lại ngay lập tức. If timeout is a number then this specifies the maximum time in seconds to block. Nếu thời gian chờ là

[1, 4, 9]
33 thì thời gian chờ vô hạn được sử dụng

Lưu ý rằng nhiều đối tượng kết nối có thể được thăm dò cùng một lúc bằng cách sử dụng

[1, 4, 9]
64

send_bytes[buffer[ , offset[ , size]]]

Send byte data from a bytes-like object as a complete message.

Nếu offset được đưa ra thì dữ liệu được đọc từ vị trí đó trong bộ đệm. Nếu kích thước được đưa ra thì nhiều byte sẽ được đọc từ bộ đệm. Bộ đệm rất lớn [khoảng 32 MiB+, mặc dù nó phụ thuộc vào hệ điều hành] có thể gây ra ngoại lệ

[1, 4, 9]
74

recv_bytes[[maxlength]]

Trả về một thông báo đầy đủ về dữ liệu byte được gửi từ đầu kia của kết nối dưới dạng chuỗi. Chặn cho đến khi có thứ gì đó để nhận. Tăng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
636 nếu không còn gì để nhận và đầu kia đã đóng

Nếu độ dài tối đa được chỉ định và thông báo dài hơn độ dài tối đa thì

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
546 sẽ được nâng lên và kết nối sẽ không thể đọc được nữa

Đã thay đổi trong phiên bản 3. 3. Hàm này từng tăng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
642, hiện là bí danh của
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
546.

recv_bytes_into[bộ đệm[ , offset]]

Đọc vào bộ đệm một thông báo đầy đủ về dữ liệu byte được gửi từ đầu kia của kết nối và trả về số byte trong thông báo. Chặn cho đến khi có thứ gì đó để nhận. Tăng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
636 nếu không còn gì để nhận và đầu kia đã đóng

bộ đệm phải là một đối tượng giống như byte có thể ghi . Nếu offset được đưa ra thì thông báo sẽ được ghi vào bộ đệm từ vị trí đó. Độ lệch phải là một số nguyên không âm nhỏ hơn độ dài của bộ đệm [tính bằng byte].

If the buffer is too short then a

[1, 4, 9]
87 exception is raised and the complete message is available as
[1, 4, 9]
88 where
[1, 4, 9]
86 is the exception instance

Changed in version 3. 3. Connection objects themselves can now be transferred between processes using

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
648 and
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
649.

Mới trong phiên bản 3. 3. Các đối tượng kết nối hiện hỗ trợ giao thức quản lý bối cảnh – xem Các loại trình quản lý bối cảnh .

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
650 trả về đối tượng kết nối và
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
651 gọi
[1, 4, 9]
75.

Ví dụ

[1, 4, 9]
9

Warning

Phương pháp

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
649 tự động giải nén dữ liệu mà nó nhận được, đây có thể là một rủi ro bảo mật trừ khi bạn có thể tin tưởng vào quy trình gửi tin nhắn

Do đó, trừ khi đối tượng kết nối được tạo bằng cách sử dụng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
43, bạn chỉ nên sử dụng các phương thức
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
46 và
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
45 sau khi thực hiện một số loại xác thực. See Authentication keys .

Warning

Nếu một quá trình bị giết trong khi nó đang cố đọc hoặc ghi vào một đường dẫn thì dữ liệu trong đường dẫn đó có khả năng bị hỏng, vì có thể không thể chắc chắn ranh giới của thông báo nằm ở đâu

Nguyên tắc đồng bộ hóa¶

Nói chung, các nguyên hàm đồng bộ hóa không cần thiết trong chương trình đa xử lý như trong chương trình đa luồng. Xem tài liệu về mô-đun

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
0

Lưu ý rằng người ta cũng có thể tạo nguyên mẫu đồng bộ hóa bằng cách sử dụng đối tượng người quản lý – xem Người quản lý .

lớp đa xử lý. Rào cản[các bên[ , action[, timeout]]]

Đối tượng rào cản. a clone of

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
658

New in version 3. 3

class multiprocessing. BoundedSemaphore[[value]]

Một đối tượng semaphore giới hạn. a close analog of

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
659

A solitary difference from its close analog exists. its

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
660 method’s first argument is named block, as is consistent with
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
661

Note

Trên macOS, điều này không thể phân biệt được với

[1, 4, 9]
17 vì
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
527 không được triển khai trên nền tảng đó

lớp đa xử lý. Điều kiện[[khóa]]

Biến điều kiện. một bí danh cho

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
664

Nếu lock được chỉ định thì nó phải là một đối tượng

[1, 4, 9]
15 hoặc
[1, 4, 9]
16 từ
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6

Đã thay đổi trong phiên bản 3. 3. Phương pháp

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
668 đã được thêm vào.

lớp đa xử lý. Sự kiện

Một bản sao của

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
669

lớp đa xử lý. Khóa

Một đối tượng khóa không đệ quy. một tương tự gần của

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
670. Khi một quy trình hoặc luồng đã nhận được khóa, các nỗ lực tiếp theo để lấy khóa đó từ bất kỳ quy trình hoặc luồng nào sẽ bị chặn cho đến khi khóa được giải phóng; . Các khái niệm và hành vi của
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
670 khi nó áp dụng cho luồng được sao chép ở đây trong
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
672 vì nó áp dụng cho cả quy trình hoặc luồng, ngoại trừ như đã lưu ý

Lưu ý rằng

[1, 4, 9]
15 thực sự là một hàm xuất xưởng trả về một thể hiện của
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
674 được khởi tạo với ngữ cảnh mặc định

[1, 4, 9]
15 hỗ trợ giao thức trình quản lý ngữ cảnh và do đó có thể được sử dụng trong câu lệnh
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
676.

mua lại[chặn=Đúng, timeout=None]

Nhận khóa, chặn hoặc không chặn

Với đối số khối được đặt thành

[1, 4, 9]
39 [mặc định], lệnh gọi phương thức sẽ chặn cho đến khi khóa ở trạng thái mở khóa, sau đó đặt thành bị khóa và trả về
[1, 4, 9]
39. Lưu ý rằng tên của đối số đầu tiên này khác với tên trong
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
679

Với đối số khối được đặt thành

[1, 4, 9]
40, lệnh gọi phương thức không chặn. Nếu khóa hiện đang ở trạng thái khóa, hãy trả lại
[1, 4, 9]
40;

Khi được gọi với giá trị dương, dấu chấm động cho thời gian chờ, hãy chặn tối đa số giây được chỉ định theo thời gian chờ miễn là không thể lấy được khóa. Các yêu cầu có giá trị âm cho thời gian chờ tương đương với thời gian chờ bằng 0. Các yêu cầu có giá trị thời gian chờ là

[1, 4, 9]
33 [mặc định] đặt khoảng thời gian chờ thành vô hạn. Lưu ý rằng cách xử lý giá trị âm hoặc giá trị
[1, 4, 9]
33 cho thời gian chờ khác với hành vi đã triển khai trong
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
679. Đối số thời gian chờ không có ý nghĩa thực tế nếu đối số khối được đặt thành
[1, 4, 9]
40 và do đó bị bỏ qua. Trả về
[1, 4, 9]
39 nếu đã lấy được khóa hoặc
[1, 4, 9]
40 nếu hết thời gian chờ

bản phát hành[]

Phát hành một khóa. Điều này có thể được gọi từ bất kỳ quy trình hoặc luồng nào, không chỉ quy trình hoặc luồng ban đầu có khóa

Hành vi giống như trong

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
689 ngoại trừ khi được gọi trên khóa không khóa, một
[1, 4, 9]
74 được nâng lên

lớp đa xử lý. RLock

Một đối tượng khóa đệ quy. một tương tự gần của

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
691. Khóa đệ quy phải được giải phóng bởi quy trình hoặc luồng đã nhận được nó. Khi một quy trình hoặc luồng đã nhận được khóa đệ quy, cùng một quy trình hoặc luồng đó có thể lấy lại nó mà không bị chặn;

Lưu ý rằng

[1, 4, 9]
16 thực sự là một hàm xuất xưởng trả về một thể hiện của
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
693 được khởi tạo với ngữ cảnh mặc định

[1, 4, 9]
16 hỗ trợ giao thức trình quản lý ngữ cảnh và do đó có thể được sử dụng trong câu lệnh
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
676.

mua lại[chặn=Đúng, timeout=None]

Nhận khóa, chặn hoặc không chặn

Khi được gọi với đối số khối được đặt thành

[1, 4, 9]
39, hãy chặn cho đến khi khóa ở trạng thái không khóa [không thuộc sở hữu của bất kỳ quy trình hoặc luồng nào] trừ khi khóa đã được sở hữu bởi quy trình hoặc luồng hiện tại. The current process or thread then takes ownership of the lock [if it does not already have ownership] and the recursion level inside the lock increments by one, resulting in a return value of
[1, 4, 9]
39. Lưu ý rằng có một số điểm khác biệt trong hành vi của đối số đầu tiên này so với cách triển khai của
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
698, bắt đầu từ tên của chính đối số đó

Khi được gọi với đối số khối được đặt thành

[1, 4, 9]
40, không chặn. Nếu khóa đã được mua [và do đó được sở hữu] bởi một quy trình hoặc luồng khác, thì quy trình hoặc luồng hiện tại không có quyền sở hữu và mức đệ quy trong khóa không bị thay đổi, dẫn đến giá trị trả về là
[1, 4, 9]
40. Nếu khóa ở trạng thái không khóa, quy trình hoặc luồng hiện tại sẽ có quyền sở hữu và mức đệ quy được tăng lên, dẫn đến giá trị trả về là
[1, 4, 9]
39

Use and behaviors of the timeout argument are the same as in

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
661. Lưu ý rằng một số hành vi hết thời gian này khác với các hành vi đã triển khai trong
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
698

bản phát hành[]

Phát hành khóa, giảm mức đệ quy. Nếu sau khi giảm, mức đệ quy bằng 0, hãy đặt lại khóa thành mở khóa [không thuộc sở hữu của bất kỳ quy trình hoặc luồng nào] và nếu bất kỳ quy trình hoặc luồng nào khác bị chặn chờ khóa được mở khóa, hãy cho phép chính xác một trong số chúng tiếp tục. Nếu sau khi giảm, mức đệ quy vẫn khác không, thì khóa vẫn bị khóa và thuộc sở hữu của quy trình gọi hoặc luồng

Chỉ gọi phương thức này khi quá trình gọi hoặc luồng sở hữu khóa. Một

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
538 được nâng lên nếu phương thức này được gọi bởi một quy trình hoặc luồng không phải là chủ sở hữu hoặc nếu khóa ở trạng thái mở khóa [không có chủ sở hữu]. Lưu ý rằng loại ngoại lệ được đưa ra trong tình huống này khác với hành vi được triển khai trong
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
505

lớp đa xử lý. Semaphore[[giá trị]]

Một đối tượng semaphore. một tương tự gần của

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
506

A solitary difference from its close analog exists. its

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
660 method’s first argument is named block, as is consistent with
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
661

Note

Trên macOS,

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
509 không được hỗ trợ, vì vậy, việc gọi
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
510 khi hết thời gian chờ sẽ mô phỏng hành vi của chức năng đó bằng cách sử dụng vòng lặp ngủ

Note

Nếu tín hiệu SIGINT được tạo bởi Ctrl-C đến trong khi luồng chính bị chặn bởi lệnh gọi tới

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
511,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
661,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
513,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
514,
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
515 hoặc
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
516 thì cuộc gọi sẽ bị gián đoạn ngay lập tức và
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
517 sẽ được nâng lên

Điều này khác với hành vi của

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
0 trong đó SIGINT sẽ bị bỏ qua trong khi các cuộc gọi chặn tương đương đang diễn ra

Note

Một số chức năng của gói này yêu cầu triển khai semaphore được chia sẻ chức năng trên hệ điều hành máy chủ. Nếu không có, mô-đun

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
519 sẽ bị vô hiệu hóa và cố gắng nhập nó sẽ dẫn đến lỗi
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
557. Xem bpo-3770 để biết thêm thông tin

Đối tượng
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
521 được chia sẻ¶

Có thể tạo các đối tượng dùng chung bằng bộ nhớ dùng chung có thể được kế thừa bởi các tiến trình con

đa xử lý. Giá trị[typecode_or_type , *args, lock=True]

Trả về một đối tượng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
521 được phân bổ từ bộ nhớ dùng chung. Theo mặc định, giá trị trả về thực sự là một trình bao bọc được đồng bộ hóa cho đối tượng. Bản thân đối tượng có thể được truy cập thông qua thuộc tính giá trị của
[1, 4, 9]
00

typecode_or_type xác định loại đối tượng được trả về. nó là loại ctypes hoặc mã loại một ký tự thuộc loại được sử dụng bởi mô-đun

[1, 4, 9]
06. *args được chuyển đến hàm tạo cho loại

Nếu khóa là

[1, 4, 9]
39 [mặc định] thì một đối tượng khóa đệ quy mới được tạo để đồng bộ hóa quyền truy cập vào giá trị. Nếu khóa là đối tượng
[1, 4, 9]
15 hoặc
[1, 4, 9]
16 thì đối tượng đó sẽ được sử dụng để đồng bộ hóa quyền truy cập vào giá trị. Nếu khóa là
[1, 4, 9]
40 thì quyền truy cập vào đối tượng được trả về sẽ không được khóa tự động bảo vệ, vì vậy nó không nhất thiết phải là “quy trình an toàn”

Các hoạt động như

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
529 liên quan đến đọc và viết không phải là nguyên tử. Vì vậy, nếu, ví dụ, bạn muốn tăng nguyên tử một giá trị được chia sẻ thì không đủ để làm

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
50

Giả sử khóa được liên kết là đệ quy [theo mặc định], thay vào đó, bạn có thể làm

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
51

Lưu ý rằng khóa là đối số chỉ có từ khóa

đa xử lý. Mảng[typecode_or_type , size_or_initializer, *, lock=True]

Trả về một mảng ctypes được phân bổ từ bộ nhớ dùng chung. Theo mặc định, giá trị trả về thực sự là một trình bao bọc được đồng bộ hóa cho mảng

typecode_or_type xác định loại phần tử của mảng được trả về. nó là loại ctypes hoặc mã loại một ký tự thuộc loại được sử dụng bởi mô-đun

[1, 4, 9]
06. Nếu size_or_initializer là một số nguyên, thì nó xác định độ dài của mảng và ban đầu mảng sẽ bằng 0. Mặt khác, size_or_initializer là một chuỗi được sử dụng để khởi tạo mảng và độ dài của nó xác định độ dài của mảng

Nếu khóa là

[1, 4, 9]
39 [mặc định] thì một đối tượng khóa mới được tạo để đồng bộ hóa quyền truy cập vào giá trị. Nếu khóa là đối tượng
[1, 4, 9]
15 hoặc
[1, 4, 9]
16 thì đối tượng đó sẽ được sử dụng để đồng bộ hóa quyền truy cập vào giá trị. Nếu khóa là
[1, 4, 9]
40 thì quyền truy cập vào đối tượng được trả về sẽ không được khóa tự động bảo vệ, vì vậy nó không nhất thiết phải là “quy trình an toàn”

Lưu ý rằng khóa chỉ là một đối số từ khóa

Lưu ý rằng một mảng của

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
535 có các thuộc tính giá trị và thô cho phép một người sử dụng nó để lưu trữ và truy xuất các chuỗi

Mô-đun
[1, 4, 9]
09¶

Mô-đun

[1, 4, 9]
09 cung cấp các hàm để cấp phát các đối tượng
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
521 từ bộ nhớ dùng chung có thể được kế thừa bởi các tiến trình con

Note

Mặc dù có thể lưu trữ một con trỏ trong bộ nhớ dùng chung, hãy nhớ rằng con trỏ này sẽ đề cập đến một vị trí trong không gian địa chỉ của một quy trình cụ thể. Tuy nhiên, con trỏ rất có thể không hợp lệ trong ngữ cảnh của quy trình thứ hai và việc cố gắng hủy đăng ký con trỏ khỏi quy trình thứ hai có thể gây ra sự cố

đa xử lý. sharedctypes. RawArray[typecode_or_type , size_or_initializer]

Trả về một mảng ctypes được phân bổ từ bộ nhớ dùng chung

typecode_or_type xác định loại phần tử của mảng được trả về. nó là loại ctypes hoặc mã loại một ký tự thuộc loại được sử dụng bởi mô-đun

[1, 4, 9]
06. Nếu size_or_initializer là một số nguyên thì nó xác định độ dài của mảng và ban đầu mảng sẽ bằng 0. Mặt khác, size_or_initializer là một chuỗi được sử dụng để khởi tạo mảng và độ dài của nó xác định độ dài của mảng

Lưu ý rằng cài đặt và nhận một phần tử có khả năng không phải là nguyên tử – thay vào đó hãy sử dụng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
540 để đảm bảo rằng quyền truy cập được tự động đồng bộ hóa bằng cách sử dụng khóa

đa xử lý. sharedctypes. Giá trị thô[typecode_or_type , *args]

Trả về một đối tượng ctypes được phân bổ từ bộ nhớ dùng chung

typecode_or_type xác định loại đối tượng được trả về. nó là loại ctypes hoặc mã loại một ký tự thuộc loại được sử dụng bởi mô-đun

[1, 4, 9]
06. *args được chuyển đến hàm tạo cho loại

Lưu ý rằng cài đặt và nhận giá trị có khả năng không phải là nguyên tử – thay vào đó hãy sử dụng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
542 để đảm bảo rằng quyền truy cập được tự động đồng bộ hóa bằng cách sử dụng khóa

Lưu ý rằng một mảng của

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
535 có các thuộc tính
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
544 và
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
545 cho phép một người sử dụng nó để lưu trữ và truy xuất các chuỗi – xem tài liệu về
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
521

đa xử lý. sharedctypes. Mảng[typecode_or_type , size_or_initializer, *, lock=True]

Giống như

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
547 ngoại trừ tùy thuộc vào giá trị của khóa, trình bao bọc đồng bộ hóa an toàn cho quy trình có thể được trả về thay vì một mảng ctypes thô

Nếu khóa là

[1, 4, 9]
39 [mặc định] thì một đối tượng khóa mới được tạo để đồng bộ hóa quyền truy cập vào giá trị. Nếu khóa là đối tượng
[1, 4, 9]
15 hoặc
[1, 4, 9]
16 thì đối tượng đó sẽ được sử dụng để đồng bộ hóa quyền truy cập vào giá trị. Nếu khóa là
[1, 4, 9]
40 thì quyền truy cập vào đối tượng được trả về sẽ không được khóa tự động bảo vệ, vì vậy nó không nhất thiết phải là “quy trình an toàn”

Lưu ý rằng khóa là đối số chỉ có từ khóa

đa xử lý. sharedctypes. Giá trị[typecode_or_type , *args, lock=True]

Giống như

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
552 ngoại trừ tùy thuộc vào giá trị của khóa, trình bao bọc đồng bộ hóa an toàn cho quy trình có thể được trả về thay vì đối tượng ctypes thô

Nếu khóa là

[1, 4, 9]
39 [mặc định] thì một đối tượng khóa mới được tạo để đồng bộ hóa quyền truy cập vào giá trị. Nếu khóa là đối tượng
[1, 4, 9]
15 hoặc
[1, 4, 9]
16 thì đối tượng đó sẽ được sử dụng để đồng bộ hóa quyền truy cập vào giá trị. Nếu khóa là
[1, 4, 9]
40 thì quyền truy cập vào đối tượng được trả về sẽ không được khóa tự động bảo vệ, vì vậy nó không nhất thiết phải là “quy trình an toàn”

Lưu ý rằng khóa là đối số chỉ có từ khóa

đa xử lý. sharedctypes. bản sao[obj]

Trả về đối tượng ctypes được cấp phát từ bộ nhớ dùng chung, đây là bản sao của đối tượng ctypes obj

đa xử lý. sharedctypes. đồng bộ[obj[ , lock]]

Trả về một đối tượng trình bao an toàn cho một đối tượng ctypes sử dụng khóa để đồng bộ hóa quyền truy cập. Nếu khóa là

[1, 4, 9]
33 [mặc định] thì đối tượng
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
558 được tạo tự động

Một trình bao bọc được đồng bộ hóa sẽ có hai phương thức ngoài các phương thức của đối tượng mà nó bao bọc.

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
559 trả về đối tượng được bọc và
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
560 trả về đối tượng khóa được sử dụng để đồng bộ hóa

Lưu ý rằng việc truy cập đối tượng ctypes thông qua trình bao bọc có thể chậm hơn rất nhiều so với truy cập đối tượng ctypes thô

Đã thay đổi trong phiên bản 3. 5. Các đối tượng được đồng bộ hóa hỗ trợ giao thức trình quản lý ngữ cảnh .

Bảng bên dưới so sánh cú pháp tạo các đối tượng ctypes dùng chung từ bộ nhớ dùng chung với cú pháp ctypes bình thường. [Trong bảng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
561 là một phân lớp nào đó của
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
562. ]

ctypes

sharedctypes sử dụng loại

sharedctypes sử dụng mã kiểu

c_double[2. 4]

RawValue[c_double, 2. 4]

RawValue['d', 2. 4]

Cấu trúc của tôi[4, 6]

RawValue[MyStruct, 4, 6]

[c_short * 7][]

RawArray[c_short, 7]

RawArray[‘h’, 7]

[c_int * 3][9, 2, 8]

RawArray[c_int, [9, 2, 8]]

RawArray['i', [9, 2, 8]]

Dưới đây là một ví dụ trong đó một số đối tượng ctypes được sửa đổi bởi một tiến trình con

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
52

Kết quả in ra là

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
53

Người quản lý¶

Trình quản lý cung cấp cách tạo dữ liệu có thể được chia sẻ giữa các quy trình khác nhau, bao gồm chia sẻ qua mạng giữa các quy trình chạy trên các máy khác nhau. A manager object controls a server process which manages shared objects. Các quy trình khác có thể truy cập các đối tượng được chia sẻ bằng cách sử dụng proxy

multiprocessing. Manager[]

Trả về một đối tượng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
563 đã bắt đầu có thể được sử dụng để chia sẻ các đối tượng giữa các quy trình. Đối tượng trình quản lý được trả về tương ứng với một tiến trình con được sinh ra và có các phương thức sẽ tạo các đối tượng được chia sẻ và trả về các proxy tương ứng

Các quy trình quản lý sẽ bị tắt ngay khi chúng được thu gom rác hoặc quy trình mẹ của chúng thoát ra. Các lớp người quản lý được định nghĩa trong mô-đun

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
564

lớp đa xử lý. quản lý. Trình quản lý cơ sở[địa chỉ=Không có . 0, authkey=None, serializer='pickle', ctx=None, *, shutdown_timeout=1.0]

Tạo đối tượng BaseManager

Sau khi tạo, người ta nên gọi

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
3 hoặc
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
566 để đảm bảo rằng đối tượng người quản lý đề cập đến quy trình người quản lý đã bắt đầu

address là địa chỉ mà quá trình quản lý lắng nghe các kết nối mới. Nếu địa chỉ là

[1, 4, 9]
33 thì một địa chỉ tùy ý được chọn

authkey là khóa xác thực sẽ được sử dụng để kiểm tra tính hợp lệ của các kết nối đến quy trình máy chủ. Nếu authkey là

[1, 4, 9]
33 thì
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
569 được sử dụng. Nếu không thì authkey được sử dụng và nó phải là một chuỗi byte

bộ nối tiếp phải là

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
570 [sử dụng tuần tự hóa
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
571] hoặc
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
572 [sử dụng tuần tự hóa
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
573]

ctx là một đối tượng ngữ cảnh hoặc

[1, 4, 9]
33 [sử dụng ngữ cảnh hiện tại]. Xem hàm
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
25

shutdown_timeout is a timeout in seconds used to wait until the process used by the manager completes in the

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
576 method. Nếu hết thời gian tắt máy, quá trình kết thúc. Nếu kết thúc quá trình cũng hết thời gian, quá trình bị hủy

Đã thay đổi trong phiên bản 3. 11. Đã thêm thông số shutdown_timeout.

start[[trình khởi tạo[, initargs]]]

Bắt đầu một quy trình con để khởi động trình quản lý. Nếu trình khởi tạo không phải là

[1, 4, 9]
33 thì quy trình con sẽ gọi
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
578 khi nó bắt đầu

get_server[]

Trả về một đối tượng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
579 đại diện cho máy chủ thực dưới sự kiểm soát của Trình quản lý. Đối tượng
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
579 hỗ trợ phương thức
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
581

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
54

Ngoài ra,

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
579 còn có thuộc tính
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
583

kết nối[]

Kết nối đối tượng quản lý cục bộ với quy trình quản lý từ xa

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
55

tắt máy[]

Dừng quá trình được sử dụng bởi người quản lý. Điều này chỉ khả dụng nếu

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
3 đã được sử dụng để bắt đầu quá trình máy chủ

Điều này có thể được gọi nhiều lần

đăng ký[typeid[ , callable[, proxytype[, exposed[, method_to_typeid[, create_method]]]]]]

Một phương thức lớp có thể được sử dụng để đăng ký một loại hoặc có thể gọi được với lớp người quản lý

typeid là một "định danh loại" được sử dụng để xác định một loại đối tượng được chia sẻ cụ thể. Đây phải là một chuỗi

có thể gọi được là một có thể gọi được sử dụng để tạo các đối tượng cho loại định danh này. Nếu một phiên bản trình quản lý sẽ được kết nối với máy chủ bằng phương thức

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
585 hoặc nếu đối số phương thức tạo_tạo là
[1, 4, 9]
40 thì điều này có thể được để lại là
[1, 4, 9]
33

proxytype là một lớp con của

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
588 được sử dụng để tạo proxy cho các đối tượng dùng chung với typeid này. Nếu
[1, 4, 9]
33 thì một lớp proxy được tạo tự động

được sử dụng để chỉ định một chuỗi tên phương thức mà proxy cho typeid này sẽ được phép truy cập bằng cách sử dụng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
590. [Nếu tiếp xúc là
[1, 4, 9]
33 thì
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
592 được sử dụng thay thế nếu nó tồn tại. ] Trong trường hợp không có danh sách hiển thị nào được chỉ định, tất cả các “phương thức công khai” của đối tượng được chia sẻ sẽ có thể truy cập được. [Ở đây “phương thức công khai” có nghĩa là bất kỳ thuộc tính nào có phương thức
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
593 và tên của nó không bắt đầu bằng
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
594. ]

method_to_typeid là ánh xạ được sử dụng để chỉ định kiểu trả về của các phương thức được hiển thị đó sẽ trả về proxy. Nó ánh xạ các tên phương thức thành các chuỗi typeid. [Nếu method_to_typeid là

[1, 4, 9]
33 thì
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
596 được sử dụng thay thế nếu nó tồn tại. ] Nếu tên của phương thức không phải là khóa của ánh xạ này hoặc nếu ánh xạ là
[1, 4, 9]
33 thì đối tượng được phương thức trả về sẽ được sao chép theo giá trị

create_method xác định xem một phương thức có nên được tạo với tên typeid có thể được sử dụng để báo cho quy trình máy chủ tạo một đối tượng dùng chung mới và trả về proxy cho nó hay không. Theo mặc định, nó là

[1, 4, 9]
39

Phiên bản

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
599 cũng có một thuộc tính chỉ đọc

địa chỉ

Địa chỉ được sử dụng bởi người quản lý

Đã thay đổi trong phiên bản 3. 3. Các đối tượng trình quản lý hỗ trợ giao thức quản lý ngữ cảnh – xem Các loại trình quản lý ngữ cảnh .

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
650 bắt đầu quá trình máy chủ [nếu nó chưa bắt đầu] và sau đó trả về đối tượng người quản lý.
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
651 cuộc gọi
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
576.

Trong các phiên bản trước,

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
650 không bắt đầu quy trình máy chủ của người quản lý nếu nó chưa được bắt đầu

lớp đa xử lý. quản lý. Trình quản lý đồng bộ hóa

Một lớp con của

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
599 có thể được sử dụng để đồng bộ hóa các quy trình. Các đối tượng thuộc loại này được trả về bởi
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
05

Các phương thức của nó tạo và trả về Đối tượng proxy cho một số loại dữ liệu thường được sử dụng để đồng bộ hóa giữa các quy trình. Điều này đáng chú ý bao gồm các danh sách và từ điển được chia sẻ.

Rào cản[các bên[ , action[, timeout]]]

Tạo một đối tượng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
658 được chia sẻ và trả về một proxy cho nó

New in version 3. 3

BoundedSemaphore[[giá trị]]

Tạo một đối tượng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
659 được chia sẻ và trả về một proxy cho nó

Điều kiện[[khóa]]

Tạo một đối tượng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
664 được chia sẻ và trả về một proxy cho nó

Nếu lock được cung cấp thì nó phải là proxy cho đối tượng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
670 hoặc
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
691

Đã thay đổi trong phiên bản 3. 3. Phương pháp

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
668 đã được thêm vào.

Sự kiện[]

Tạo một đối tượng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
669 được chia sẻ và trả về một proxy cho nó

Khóa[]

Tạo một đối tượng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
670 được chia sẻ và trả về một proxy cho nó

Không gian tên[]

Tạo đối tượng

[1, 4, 9]
14 được chia sẻ và trả lại proxy cho đối tượng đó

Hàng đợi[[kích thước tối đa]]

Tạo một đối tượng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
42 được chia sẻ và trả về một proxy cho nó

RLock[]

Tạo một đối tượng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
691 được chia sẻ và trả về một proxy cho nó

Semaphore[[giá trị]]

Tạo một đối tượng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
506 được chia sẻ và trả về một proxy cho nó

Mảng[mã loại , trình tự]

Tạo một mảng và trả về một proxy cho nó

Giá trị[mã loại , giá trị]

Tạo một đối tượng có thuộc tính

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
544 có thể ghi và trả về một proxy cho đối tượng đó

dict[]dict[mapping]dict[sequence]

Tạo đối tượng

[1, 4, 9]
13 được chia sẻ và trả lại proxy cho đối tượng đó

danh sách[]danh sách[sequence]

Tạo đối tượng

[1, 4, 9]
12 được chia sẻ và trả lại proxy cho đối tượng đó

Đã thay đổi trong phiên bản 3. 6. Các đối tượng được chia sẻ có khả năng được lồng vào nhau. For example, a shared container object such as a shared list can contain other shared objects which will all be managed and synchronized by the

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
563.

lớp đa xử lý. quản lý. Không gian tên

Một loại có thể đăng ký với

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
563

Một đối tượng không gian tên không có phương thức công khai, nhưng có các thuộc tính có thể ghi. Đại diện của nó cho thấy các giá trị của các thuộc tính của nó

Tuy nhiên, khi sử dụng proxy cho đối tượng không gian tên, thuộc tính bắt đầu bằng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
594 sẽ là thuộc tính của proxy chứ không phải thuộc tính của tham chiếu

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
56

Trình quản lý tùy chỉnh¶

Để tạo trình quản lý của riêng mình, một người tạo một lớp con của

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
599 và sử dụng phương thức lớp
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
25 để đăng ký các loại hoặc khả năng gọi mới với lớp trình quản lý. Ví dụ

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
57

Sử dụng trình quản lý từ xa¶

Có thể chạy máy chủ quản lý trên một máy và để khách hàng sử dụng nó từ các máy khác [giả sử rằng tường lửa có liên quan cho phép điều đó]

Chạy các lệnh sau sẽ tạo một máy chủ cho một hàng đợi được chia sẻ duy nhất mà các máy khách từ xa có thể truy cập

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
58

Một khách hàng có thể truy cập máy chủ như sau

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
59

Một khách hàng khác cũng có thể sử dụng nó

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
60

Các quy trình cục bộ cũng có thể truy cập hàng đợi đó, sử dụng mã từ phía trên trên máy khách để truy cập từ xa

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
61

Đối tượng ủy quyền¶

Proxy là một đối tượng đề cập đến một đối tượng được chia sẻ tồn tại [có lẽ] trong một quy trình khác. Đối tượng được chia sẻ được cho là tham chiếu của proxy. Nhiều đối tượng proxy có thể có cùng một tham chiếu

Một đối tượng proxy có các phương thức gọi các phương thức tương ứng của tham chiếu của nó [mặc dù không phải mọi phương thức của tham chiếu đều nhất thiết phải có sẵn thông qua proxy]. Bằng cách này, một proxy có thể được sử dụng giống như người tham chiếu của nó có thể

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
62

Lưu ý rằng việc áp dụng

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
26 cho proxy sẽ trả về biểu diễn của tham chiếu, trong khi áp dụng
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
27 sẽ trả về biểu diễn của proxy

Một tính năng quan trọng của các đối tượng proxy là chúng có thể chọn được để chúng có thể được chuyển giữa các quy trình. Như vậy, một tham chiếu có thể chứa Đối tượng proxy . Điều này cho phép lồng các danh sách được quản lý này, lệnh và các Đối tượng proxy khác.

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
63

Tương tự, proxy dict và list có thể được lồng vào nhau

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
64

Nếu các đối tượng tiêu chuẩn [không phải proxy]

[1, 4, 9]
12 hoặc
[1, 4, 9]
13 được chứa trong một tham chiếu, các sửa đổi đối với các giá trị có thể thay đổi đó sẽ không được truyền qua trình quản lý vì proxy không có cách nào biết khi nào các giá trị chứa trong đó được sửa đổi. Tuy nhiên, việc lưu trữ một giá trị trong proxy vùng chứa [kích hoạt
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
30 trên đối tượng proxy] sẽ lan truyền qua trình quản lý và do đó, để sửa đổi mục đó một cách hiệu quả, người ta có thể gán lại giá trị đã sửa đổi cho proxy vùng chứa

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
65

Cách tiếp cận này có lẽ kém thuận tiện hơn so với việc sử dụng Đối tượng proxy lồng nhau cho hầu hết các trường hợp sử dụng nhưng cũng thể hiện mức độ kiểm soát đối với quá trình đồng bộ hóa.

Note

Các loại proxy trong

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 không làm gì để hỗ trợ so sánh theo giá trị. Vì vậy, ví dụ, chúng ta có

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
66

Thay vào đó, người ta chỉ nên sử dụng một bản sao của người giới thiệu khi so sánh

lớp đa xử lý. quản lý. Proxy cơ sở

Các đối tượng proxy là thể hiện của các lớp con của

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
588

_callmethod[tên phương thức[ , args[, kwds]]]

Gọi và trả về kết quả của một phương thức tham chiếu của proxy

Nếu

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
33 là proxy có tham chiếu là
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
34 thì biểu thức

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
67

sẽ đánh giá biểu thức

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
68

trong quy trình của nhà quản lý

Giá trị được trả về sẽ là một bản sao kết quả của cuộc gọi hoặc một proxy cho một đối tượng được chia sẻ mới – xem tài liệu về đối số method_to_typeid của

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
35

Nếu một ngoại lệ được đưa ra bởi cuộc gọi, thì sẽ được đưa ra lại bởi

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
36. Nếu một số ngoại lệ khác được đưa ra trong quy trình của người quản lý thì điều này được chuyển đổi thành ngoại lệ
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
37 và được đưa ra bởi
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
36

Đặc biệt lưu ý rằng một ngoại lệ sẽ được nêu ra nếu tên phương thức chưa được hiển thị

Một ví dụ về việc sử dụng

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
36

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
69

_getvalue[]

Trả lại một bản sao của người giới thiệu

If the referent is unpicklable then this will raise an exception

__repr__[]

Trả về một đại diện của đối tượng proxy

__str__[]

Trả về đại diện của tham chiếu

Dọn dẹp¶

Một đối tượng proxy sử dụng một cuộc gọi lại yếu để khi nó được thu gom rác, nó sẽ tự hủy đăng ký khỏi trình quản lý sở hữu tham chiếu của nó

Một đối tượng được chia sẻ sẽ bị xóa khỏi quy trình quản lý khi không còn bất kỳ proxy nào đề cập đến nó

Nhóm quy trình¶

Người ta có thể tạo một nhóm các quy trình sẽ thực hiện các tác vụ được gửi cho nó với lớp

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
5

lớp đa xử lý. hồ bơi. Nhóm[[quy trình[, initializer[, initargs[, maxtasksperchild[, context]]]]]]

Một đối tượng nhóm quy trình kiểm soát nhóm quy trình công nhân mà công việc có thể được gửi tới. Nó hỗ trợ các kết quả không đồng bộ với thời gian chờ và gọi lại và có triển khai bản đồ song song

quy trình là số lượng quy trình công nhân để sử dụng. Nếu các quy trình là

[1, 4, 9]
33 thì số được trả về bởi
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
579 được sử dụng

Nếu trình khởi tạo không phải là

[1, 4, 9]
33 thì mỗi worker process sẽ gọi
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
578 khi nó bắt đầu

maxtasksperchild là số lượng tác vụ mà một worker process có thể hoàn thành trước khi nó thoát và được thay thế bằng một worker process mới, để cho phép giải phóng các tài nguyên không sử dụng. Maxtaskperchild mặc định là

[1, 4, 9]
33, có nghĩa là worker process sẽ tồn tại miễn là pool

bối cảnh có thể được sử dụng để chỉ định bối cảnh được sử dụng để bắt đầu các quy trình worker. Thông thường, một nhóm được tạo bằng cách sử dụng hàm

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
46 hoặc phương thức
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
47 của một đối tượng bối cảnh. Trong cả hai trường hợp bối cảnh được thiết lập một cách thích hợp

Lưu ý rằng các phương thức của đối tượng nhóm chỉ nên được gọi bởi quá trình tạo nhóm

Warning

Các đối tượng

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
48 có tài nguyên nội bộ cần được quản lý đúng cách [giống như bất kỳ tài nguyên nào khác] bằng cách sử dụng nhóm làm trình quản lý bối cảnh hoặc bằng cách gọi thủ công
[1, 4, 9]
75 và
[1, 4, 9]
71. Không làm điều này có thể dẫn đến quá trình bị treo khi quyết toán

Lưu ý rằng việc dựa vào trình thu gom rác để phá hủy nhóm là không đúng vì CPython không đảm bảo rằng trình hoàn thiện của nhóm sẽ được gọi [xem

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
51 để biết thêm thông tin]

Mới trong phiên bản 3. 2. maxtaskperchild

Mới trong phiên bản 3. 4. bối cảnh

Note

Các quy trình công nhân trong một

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
5 thường tồn tại trong toàn bộ thời lượng của hàng đợi công việc của Nhóm. Một mô hình phổ biến được tìm thấy trong các hệ thống khác [chẳng hạn như Apache, mod_wsgi, v.v.] để giải phóng tài nguyên do công nhân nắm giữ là cho phép một công nhân trong nhóm chỉ hoàn thành một lượng công việc nhất định trước khi thoát, được dọn sạch và một quy trình mới được sinh ra . Đối số maxtasksperchild cho
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
5 hiển thị khả năng này cho người dùng cuối

áp dụng[chức năng[ , args[, kwds]]]

Gọi func với đối số args và đối số từ khóa kwds. Nó chặn cho đến khi kết quả sẵn sàng. Với các khối này,

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
54 phù hợp hơn để thực hiện công việc song song. Ngoài ra, func chỉ được thực thi ở một trong các công nhân của nhóm

apply_async[func[ , args[, kwds[, callback[, error_callback]]]]]

Một biến thể của phương thức

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
55 trả về một đối tượng
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
56

Nếu gọi lại được chỉ định thì nó phải là một cuộc gọi có thể chấp nhận một đối số. Khi kết quả sẵn sàng gọi lại được áp dụng cho nó, đó là trừ khi cuộc gọi không thành công, trong trường hợp đó, error_callback được áp dụng thay thế

Nếu error_callback được chỉ định thì nó phải là một đối số có thể gọi được chấp nhận một đối số. Nếu chức năng đích không thành công, thì error_callback được gọi với trường hợp ngoại lệ

Các cuộc gọi lại phải hoàn thành ngay lập tức vì nếu không thì chuỗi xử lý kết quả sẽ bị chặn

map[func , có thể lặp lại[, chunksize]]

Tương đương song song với chức năng tích hợp sẵn của

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
57 [tuy nhiên, nó chỉ hỗ trợ một đối số có thể lặp lại, đối với nhiều lần lặp lại, hãy xem
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
58]. It blocks until the result is ready

Phương pháp này cắt iterable thành một số khối mà nó gửi đến nhóm quy trình dưới dạng các tác vụ riêng biệt. Kích thước [gần đúng] của các khối này có thể được chỉ định bằng cách đặt kích thước khối thành một số nguyên dương

Lưu ý rằng nó có thể gây ra việc sử dụng bộ nhớ cao cho các lần lặp rất dài. Cân nhắc sử dụng

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
59 hoặc
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
60 với tùy chọn chunksize rõ ràng để có hiệu quả tốt hơn

map_async[func , có thể lặp lại[, chunksize[, callback[, error_callback]]]]

Một biến thể của phương thức

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
57 trả về một đối tượng
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
56

Nếu gọi lại được chỉ định thì nó phải là một cuộc gọi có thể chấp nhận một đối số. Khi kết quả sẵn sàng gọi lại được áp dụng cho nó, đó là trừ khi cuộc gọi không thành công, trong trường hợp đó, error_callback được áp dụng thay thế

Nếu error_callback được chỉ định thì nó phải là một đối số có thể gọi được chấp nhận một đối số. Nếu chức năng đích không thành công, thì error_callback được gọi với trường hợp ngoại lệ

Các cuộc gọi lại phải hoàn thành ngay lập tức vì nếu không thì chuỗi xử lý kết quả sẽ bị chặn

imap[func , có thể lặp lại[, chunksize]]

Phiên bản lười biếng hơn của

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
57

Đối số chunksize giống như đối số được sử dụng bởi phương thức

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
57. Đối với các lần lặp rất dài, sử dụng giá trị lớn cho chunksize có thể giúp công việc hoàn thành nhanh hơn nhiều so với sử dụng giá trị mặc định là
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
65

Ngoài ra, nếu chunksize là

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
65 thì phương thức
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
67 của trình vòng lặp được trả về bởi phương thức
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
59 có tham số thời gian chờ tùy chọn.
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
69 sẽ tăng
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
70 nếu kết quả không thể trả về trong thời gian chờ vài giây

imap_unordered[func , có thể lặp lại[, chunksize]]

Giống như

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
59 ngoại trừ thứ tự của kết quả từ trình vòng lặp được trả về nên được coi là tùy ý. [Chỉ khi chỉ có một worker process thì lệnh mới được đảm bảo là “đúng”. ]

starmap[func , có thể lặp lại[, chunksize]]

Giống như

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
57 ngoại trừ các phần tử của iterable dự kiến ​​​​là iterables được giải nén dưới dạng đối số

Do đó, một lần lặp của

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
73 dẫn đến
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
74

New in version 3. 3

starmap_async[func , có thể lặp lại[, chunksize[, callback[, error_callback]]]]

Một sự kết hợp của

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
58 và
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
76 lặp đi lặp lại trên iterable của iterables và gọi func với iterables được giải nén. Trả về một đối tượng kết quả

New in version 3. 3

close[]

Ngăn chặn bất kỳ nhiệm vụ nào khác được gửi đến nhóm. Khi tất cả các tác vụ đã được hoàn thành, các quy trình công nhân sẽ thoát

terminate[]

Dừng quy trình công nhân ngay lập tức mà không hoàn thành công việc chưa hoàn thành. Khi đối tượng pool được thu gom rác,

[1, 4, 9]
71 sẽ được gọi ngay lập tức

join[]

Chờ các worker process thoát ra. Người ta phải gọi

[1, 4, 9]
75 hoặc
[1, 4, 9]
71 trước khi sử dụng
[1, 4, 9]
48

Mới trong phiên bản 3. 3. Các đối tượng nhóm hiện hỗ trợ giao thức quản lý bối cảnh – xem Các loại trình quản lý bối cảnh .

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
650 trả về đối tượng pool và
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
651 gọi
[1, 4, 9]
71.

lớp đa xử lý. hồ bơi. Kết quả không đồng bộ

Lớp của kết quả trả về bởi

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
84 và
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
85

nhận[[thời gian chờ]]

Trả lại kết quả khi nó đến. Nếu thời gian chờ không phải là

[1, 4, 9]
33 và kết quả không đến trong vòng vài giây hết thời gian chờ thì
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
70 được nâng lên. Nếu cuộc gọi từ xa đưa ra một ngoại lệ thì ngoại lệ đó sẽ được đưa ra lại trước
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
560

chờ[[hết giờ]]

Đợi cho đến khi có kết quả hoặc cho đến khi hết giây

sẵn sàng[]

Quay lại xem cuộc gọi đã hoàn thành chưa

thành công[]

Trả lại xem cuộc gọi có hoàn thành mà không đưa ra ngoại lệ hay không. Sẽ tăng

[1, 4, 9]
74 nếu kết quả chưa sẵn sàng

Đã thay đổi trong phiên bản 3. 7. Nếu kết quả chưa sẵn sàng,

[1, 4, 9]
74 được nâng lên thay vì
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
538.

Ví dụ sau minh họa việc sử dụng pool

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
50

Người nghe và Khách hàng¶

Thông thường, việc chuyển thông báo giữa các quy trình được thực hiện bằng cách sử dụng hàng đợi hoặc bằng cách sử dụng các đối tượng

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
514 được trả về bởi
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
43

Tuy nhiên, mô-đun

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
94 cho phép linh hoạt hơn. Về cơ bản, nó cung cấp API định hướng thông báo cấp cao để xử lý các ổ cắm hoặc đường ống có tên Windows. Nó cũng có hỗ trợ xác thực thông báo bằng cách sử dụng mô-đun
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
95 và để thăm dò nhiều kết nối cùng một lúc

đa xử lý. sự liên quan. deliver_challenge[kết nối , mã xác thực]

Send a randomly generated message to the other end of the connection and wait for a reply

Nếu câu trả lời khớp với thông báo của tin nhắn sử dụng authkey làm khóa thì tin nhắn chào mừng sẽ được gửi đến đầu kia của kết nối. Nếu không thì

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
96 được nâng lên

đa xử lý. sự liên quan. answer_challenge[kết nối , mã xác thực]

Nhận một tin nhắn, tính toán thông báo của tin nhắn bằng authkey làm khóa, sau đó gửi lại thông báo

Nếu không nhận được tin nhắn chào mừng, thì

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
96 được nâng lên

đa xử lý. sự liên quan. Khách hàng[địa chỉ[ , family[, authkey]]]

Cố gắng thiết lập kết nối với người nghe đang sử dụng địa chỉ address, trả về

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
514

Loại kết nối được xác định bởi đối số họ, nhưng điều này thường có thể được bỏ qua vì nó thường có thể được suy ra từ định dạng địa chỉ. [Xem Định dạng địa chỉ ]

Nếu authkey được cung cấp và không phải là Không có, thì đó phải là một chuỗi byte và sẽ được sử dụng làm khóa bí mật cho thử thách xác thực dựa trên HMAC. Không có xác thực nào được thực hiện nếu authkey là Không có.

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
96 được nâng lên nếu xác thực không thành công. Xem Khóa xác thực .

lớp đa xử lý. sự liên quan. Người nghe[[địa chỉ[, family[, backlog[, authkey]]]]]

Trình bao bọc cho ổ cắm bị ràng buộc hoặc đường ống có tên Windows đang 'lắng nghe' các kết nối

địa chỉ là địa chỉ được sử dụng bởi ổ cắm bị ràng buộc hoặc đường ống có tên của đối tượng người nghe

Note

Nếu một địa chỉ của '0. 0. 0. 0' được sử dụng, địa chỉ sẽ không phải là điểm cuối có thể kết nối trên Windows. If you require a connectable end-point, you should use ‘127. 0. 0. 1’

họ là loại ổ cắm [hoặc ống có tên] sẽ sử dụng. Đây có thể là một trong các chuỗi

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
00 [đối với ổ cắm TCP],
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
01 [đối với ổ cắm tên miền Unix] hoặc
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
02 [đối với đường ống có tên Windows]. Of these only the first is guaranteed to be available. If family is
[1, 4, 9]
33 then the family is inferred from the format of address. If address is also
[1, 4, 9]
33 then a default is chosen. This default is the family which is assumed to be the fastest available. See Address Formats . Note that if family is
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
01 and address is
[1, 4, 9]
33 then the socket will be created in a private temporary directory created using
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
07.

If the listener object uses a socket then backlog [1 by default] is passed to the

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
08 method of the socket once it has been bound

Nếu authkey được cung cấp và không phải là Không có, thì đó phải là một chuỗi byte và sẽ được sử dụng làm khóa bí mật cho thử thách xác thực dựa trên HMAC. Không có xác thực nào được thực hiện nếu authkey là Không có.

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
96 được nâng lên nếu xác thực không thành công. Xem Khóa xác thực .

accept[]

Accept a connection on the bound socket or named pipe of the listener object and return a

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
514 object. If authentication is attempted and fails, then
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
96 is raised

close[]

Close the bound socket or named pipe of the listener object. This is called automatically when the listener is garbage collected. However it is advisable to call it explicitly

Listener objects have the following read-only properties

địa chỉ

The address which is being used by the Listener object

last_accepted

The address from which the last accepted connection came. If this is unavailable then it is

[1, 4, 9]
33

New in version 3. 3. Listener objects now support the context management protocol – see Context Manager Types .

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
650 returns the listener object, and
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
651 calls
[1, 4, 9]
75.

multiprocessing. connection. wait[object_list , timeout=None]

Wait till an object in object_list is ready. Returns the list of those objects in object_list which are ready. If timeout is a float then the call blocks for at most that many seconds. If timeout is

[1, 4, 9]
33 then it will block for an unlimited period. A negative timeout is equivalent to a zero timeout

For both Unix and Windows, an object can appear in object_list if it is

  • a readable

    from multiprocessing import Process, Queue
    
    def f[q]:
        q.put[[42, None, 'hello']]
    
    if __name__ == '__main__':
        q = Queue[]
        p = Process[target=f, args=[q,]]
        p.start[]
        print[q.get[]]    # prints "[42, None, 'hello']"
        p.join[]
    
    514 object;

  • a connected and readable

    from multiprocessing import Process, Queue
    
    def f[q]:
        q.put[[42, None, 'hello']]
    
    if __name__ == '__main__':
        q = Queue[]
        p = Process[target=f, args=[q,]]
        p.start[]
        print[q.get[]]    # prints "[42, None, 'hello']"
        p.join[]
    
    18 object; or

  • the

    from multiprocessing import Process, Queue
    
    def f[q]:
        q.put[[42, None, 'hello']]
    
    if __name__ == '__main__':
        q = Queue[]
        p = Process[target=f, args=[q,]]
        p.start[]
        print[q.get[]]    # prints "[42, None, 'hello']"
        p.join[]
    
    19 attribute of a
    from multiprocessing import Process, Pipe
    
    def f[conn]:
        conn.send[[42, None, 'hello']]
        conn.close[]
    
    if __name__ == '__main__':
        parent_conn, child_conn = Pipe[]
        p = Process[target=f, args=[child_conn,]]
        p.start[]
        print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
        p.join[]
    
    0 object

A connection or socket object is ready when there is data available to be read from it, or the other end has been closed

Unix.

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
21 almost equivalent
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
22. The difference is that, if
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
23 is interrupted by a signal, it can raise
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
546 with an error number of
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
25, whereas
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
26 will not

Windows. An item in object_list must either be an integer handle which is waitable [according to the definition used by the documentation of the Win32 function

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
27] or it can be an object with a
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
28 method which returns a socket handle or pipe handle. [Note that pipe handles and socket handles are not waitable handles. ]

New in version 3. 3

Examples

The following server code creates a listener which uses

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
29 as an authentication key. It then waits for a connection and sends some data to the client

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
51

The following code connects to the server and receives some data from the server

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
52

The following code uses

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
26 to wait for messages from multiple processes at once

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
53

Address Formats¶

  • An

    from multiprocessing import Process, Queue
    
    def f[q]:
        q.put[[42, None, 'hello']]
    
    if __name__ == '__main__':
        q = Queue[]
        p = Process[target=f, args=[q,]]
        p.start[]
        print[q.get[]]    # prints "[42, None, 'hello']"
        p.join[]
    
    00 address is a tuple of the form
    from multiprocessing import Process, Queue
    
    def f[q]:
        q.put[[42, None, 'hello']]
    
    if __name__ == '__main__':
        q = Queue[]
        p = Process[target=f, args=[q,]]
        p.start[]
        print[q.get[]]    # prints "[42, None, 'hello']"
        p.join[]
    
    32 where hostname is a string and port is an integer

  • An

    from multiprocessing import Process, Queue
    
    def f[q]:
        q.put[[42, None, 'hello']]
    
    if __name__ == '__main__':
        q = Queue[]
        p = Process[target=f, args=[q,]]
        p.start[]
        print[q.get[]]    # prints "[42, None, 'hello']"
        p.join[]
    
    01 address is a string representing a filename on the filesystem

  • An

    from multiprocessing import Process, Queue
    
    def f[q]:
        q.put[[42, None, 'hello']]
    
    if __name__ == '__main__':
        q = Queue[]
        p = Process[target=f, args=[q,]]
        p.start[]
        print[q.get[]]    # prints "[42, None, 'hello']"
        p.join[]
    
    02 address is a string of the form
    from multiprocessing import Process, Queue
    
    def f[q]:
        q.put[[42, None, 'hello']]
    
    if __name__ == '__main__':
        q = Queue[]
        p = Process[target=f, args=[q,]]
        p.start[]
        print[q.get[]]    # prints "[42, None, 'hello']"
        p.join[]
    
    35. To use
    from multiprocessing import Process, Queue
    
    def f[q]:
        q.put[[42, None, 'hello']]
    
    if __name__ == '__main__':
        q = Queue[]
        p = Process[target=f, args=[q,]]
        p.start[]
        print[q.get[]]    # prints "[42, None, 'hello']"
        p.join[]
    
    36 to connect to a named pipe on a remote computer called ServerName one should use an address of the form
    from multiprocessing import Process, Queue
    
    def f[q]:
        q.put[[42, None, 'hello']]
    
    if __name__ == '__main__':
        q = Queue[]
        p = Process[target=f, args=[q,]]
        p.start[]
        print[q.get[]]    # prints "[42, None, 'hello']"
        p.join[]
    
    37 instead

Note that any string beginning with two backslashes is assumed by default to be an

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
02 address rather than an
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
01 address

Authentication keys¶

When one uses

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
40, the data received is automatically unpickled. Unfortunately unpickling data from an untrusted source is a security risk. Therefore
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
41 and
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
36 use the
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
95 module to provide digest authentication

An authentication key is a byte string which can be thought of as a password. once a connection is established both ends will demand proof that the other knows the authentication key. [Demonstrating that both ends are using the same key does not involve sending the key over the connection. ]

If authentication is requested but no authentication key is specified then the return value of

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
569 is used [see
from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0]. This value will be automatically inherited by any
from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0 object that the current process creates. This means that [by default] all processes of a multi-process program will share a single authentication key which can be used when setting up connections between themselves

Suitable authentication keys can also be generated by using

[1, 4, 9]
61

Logging¶

Some support for logging is available. Note, however, that the

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
48 package does not use process shared locks so it is possible [depending on the handler type] for messages from different processes to get mixed up

multiprocessing. get_logger[]

Returns the logger used by

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6. If necessary, a new one will be created

When first created the logger has level

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
50 and no default handler. Messages sent to this logger will not by default propagate to the root logger

Note that on Windows child processes will only inherit the level of the parent process’s logger – any other customization of the logger will not be inherited

multiprocessing. log_to_stderr[level=None]

This function performs a call to

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
51 but in addition to returning the logger created by get_logger, it adds a handler which sends output to
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
52 using format
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
53. You can modify
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
54 of the logger by passing a
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
55 argument

Below is an example session with logging turned on

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
54

For a full table of logging levels, see the

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
48 module

The
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
57 module¶

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
57 replicates the API of
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 but is no more than a wrapper around the
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
0 module

In particular, the

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
5 function provided by
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
57 returns an instance of
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
63, which is a subclass of
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
5 that supports all the same method calls but uses a pool of worker threads rather than worker processes

class multiprocessing. pool. ThreadPool[[processes[ , initializer[ , initargs]]]]

A thread pool object which controls a pool of worker threads to which jobs can be submitted.

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
63 instances are fully interface compatible with
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
5 instances, and their resources must also be properly managed, either by using the pool as a context manager or by calling
[1, 4, 9]
75 and
[1, 4, 9]
71 manually

processes is the number of worker threads to use. If processes is

[1, 4, 9]
33 then the number returned by
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
579 is used

Nếu trình khởi tạo không phải là

[1, 4, 9]
33 thì mỗi worker process sẽ gọi
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
578 khi nó bắt đầu

Unlike

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
5, maxtasksperchild and context cannot be provided

Note

A

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
63 shares the same interface as
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
5, which is designed around a pool of processes and predates the introduction of the
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
9 module. As such, it inherits some operations that don’t make sense for a pool backed by threads, and it has its own type for representing the status of asynchronous jobs,
import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
56, that is not understood by any other libraries

Users should generally prefer to use

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
78, which has a simpler interface that was designed around threads from the start, and which returns
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
79 instances that are compatible with many other libraries, including
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
80

Programming guidelines¶

There are certain guidelines and idioms which should be adhered to when using

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6

All start methods¶

Những điều sau đây áp dụng cho tất cả các phương pháp bắt đầu

Tránh trạng thái chia sẻ

Càng nhiều càng tốt, người ta nên cố gắng tránh chuyển một lượng lớn dữ liệu giữa các quy trình

Có lẽ tốt nhất là nên sử dụng hàng đợi hoặc đường ống để liên lạc giữa các quy trình thay vì sử dụng các nguyên mẫu đồng bộ hóa cấp thấp hơn

độ chua

Đảm bảo rằng các đối số cho các phương thức của proxy có thể chọn được

Chủ đề an toàn của proxy

Không sử dụng đối tượng proxy từ nhiều luồng trừ khi bạn bảo vệ nó bằng khóa

[Không bao giờ có vấn đề với các quy trình khác nhau sử dụng cùng một proxy. ]

Joining zombie processes

On Unix when a process finishes but has not been joined it becomes a zombie. There should never be very many because each time a new process starts [or

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
82 is called] all completed processes which have not yet been joined will be joined. Also calling a finished process’s
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
83 will join the process. Even so it is probably good practice to explicitly join all the processes that you start

Better to inherit than pickle/unpickle

When using the spawn or forkserver start methods many types from

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 need to be picklable so that child processes can use them. However, one should generally avoid sending shared objects to other processes using pipes or queues. Instead you should arrange the program so that a process which needs access to a shared resource created elsewhere can inherit it from an ancestor process

Avoid terminating processes

Using the

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
85 method to stop a process is liable to cause any shared resources [such as locks, semaphores, pipes and queues] currently being used by the process to become broken or unavailable to other processes

Therefore it is probably best to only consider using

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
85 on processes which never use any shared resources

Joining processes that use queues

Bear in mind that a process that has put items in a queue will wait before terminating until all the buffered items are fed by the “feeder” thread to the underlying pipe. [The child process can call the

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
87 method of the queue to avoid this behaviour. ]

This means that whenever you use a queue you need to make sure that all items which have been put on the queue will eventually be removed before the process is joined. Otherwise you cannot be sure that processes which have put items on the queue will terminate. Remember also that non-daemonic processes will be joined automatically

An example which will deadlock is the following

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
55

A fix here would be to swap the last two lines [or simply remove the

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
88 line]

Explicitly pass resources to child processes

On Unix using the fork start method, a child process can make use of a shared resource created in a parent process using a global resource. However, it is better to pass the object as an argument to the constructor for the child process

Apart from making the code [potentially] compatible with Windows and the other start methods this also ensures that as long as the child process is still alive the object will not be garbage collected in the parent process. This might be important if some resource is freed when the object is garbage collected in the parent process

So for instance

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
56

should be rewritten as

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
57

Beware of replacing

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
89 with a “file like object”

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
6 originally unconditionally called

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
58

in the

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
91 method — this resulted in issues with processes-in-processes. This has been changed to

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
59

Which solves the fundamental issue of processes colliding with each other resulting in a bad file descriptor error, but introduces a potential danger to applications which replace

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
92 with a “file-like object” with output buffering. This danger is that if multiple processes call
[1, 4, 9]
75 on this file-like object, it could result in the same data being flushed to the object multiple times, resulting in corruption

If you write a file-like object and implement your own caching, you can make it fork-safe by storing the pid whenever you append to the cache, and discarding the cache when the pid changes. For example

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
0

For more information, see bpo-5155, bpo-5313 and bpo-5331

The spawn and forkserver start methods¶

There are a few extra restriction which don’t apply to the fork start method

More picklability

Ensure that all arguments to

[1, 4, 9]
43 are picklable. Also, if you subclass
from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
0 then make sure that instances will be picklable when the
from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
96 method is called

Global variables

Bear in mind that if code run in a child process tries to access a global variable, then the value it sees [if any] may not be the same as the value in the parent process at the time that

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
96 was called

However, global variables which are just module level constants cause no problems

Safe importing of main module

Make sure that the main module can be safely imported by a new Python interpreter without causing unintended side effects [such a starting a new process]

For example, using the spawn or forkserver start method running the following module would fail with a

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
589

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
1

Instead one should protect the “entry point” of the program by using

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
99 as follows

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
2

[The

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
588 line can be omitted if the program will be run normally instead of frozen. ]

This allows the newly spawned Python interpreter to safely import the module and then run the module’s

from multiprocessing import Process, Pipe

def f[conn]:
    conn.send[[42, None, 'hello']]
    conn.close[]

if __name__ == '__main__':
    parent_conn, child_conn = Pipe[]
    p = Process[target=f, args=[child_conn,]]
    p.start[]
    print[parent_conn.recv[]]   # prints "[42, None, 'hello']"
    p.join[]
01 function

Similar restrictions apply if a pool or manager is created in the main module

Examples¶

Demonstration of how to create and use customized managers and proxies

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
3

Using

from multiprocessing import Process, Queue

def f[q]:
    q.put[[42, None, 'hello']]

if __name__ == '__main__':
    q = Queue[]
    p = Process[target=f, args=[q,]]
    p.start[]
    print[q.get[]]    # prints "[42, None, 'hello']"
    p.join[]
5

import multiprocessing as mp

def foo[q]:
    q.put['hello']

if __name__ == '__main__':
    ctx = mp.get_context['spawn']
    q = ctx.Queue[]
    p = ctx.Process[target=foo, args=[q,]]
    p.start[]
    print[q.get[]]
    p.join[]
4

Một ví dụ cho thấy cách sử dụng hàng đợi để cung cấp các tác vụ cho một tập hợp các quy trình công nhân và thu thập kết quả

Chủ Đề