gpt4 book ai didi

python 具有共享变量和异步的多处理?

转载 作者:行者123 更新时间:2023-12-01 03:27:46 25 4
gpt4 key购买 nike

我是 python 多重处理的新手。我遇到一个算法程序。我已经坚持了快一周了。我将非常感谢您的帮助。我将展示算法。多进程间的全局变量X,k

  1. 从全局内存中读取变量X
  2. 当 k < 100 时循环(k 是共享内存计数器)

      a). k=k+1, globaly
    b). Read y from the global memory with locks
    c). compute _g=G(k,y,x)
    d). USING _g to update the y and X,globally, with locks

    结束

最佳答案

您不能跨进程共享变量或任何其他内置类型。为了跨进程共享数据,multiprocessing模块有两种类型:

  1. Queue
  2. Pipe

检查:Exchanging objects between processes文档。

<小时/>

Queue document说:

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.

根据Pipe document :

Returns a pair (conn1, conn2) of Connection objects representing the ends of a pipe.

If duplex is True (the default) then the pipe is bidirectional. If duplex is False then the pipe is unidirectional: conn1 can only be used for receiving messages and conn2 can only be used for sending messages.

关于python 具有共享变量和异步的多处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41258775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com