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 Show
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
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
ống
Đồ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
quy trình máy chủ
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]5start() ¶ 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ệtjoin([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úcdaemon ¶ 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 methodspid ¶ Return the process ID. Before the process is spawned, this will be [1, 4, 9]33exitcode ¶ 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 -Nauthkey ¶ 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]6exception 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 exceptionsexception 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 stringexception 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 .
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 messagesclass 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]48qsize() ¶ 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 implementedempty() ¶ Return [1, 4, 9]39 if the queue is empty, [1, 4, 9]40 otherwise. Because of multithreading/multiprocessing semantics, this is not reliable 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()539get([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 belowlớ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óaclose() ¶ 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]40get() ¶ 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]48task_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 queuejoin() ¶ 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()581multiprocessing. 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ựcmultiprocessing. 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ối ¶ gử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]74recv() ¶ 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 đã đóngfileno() ¶ 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]64send_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]74recv_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()669lớ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ênlớ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()698bả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()505lớ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 |