Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多处理共享变量(自建类型)_Python - Fatal编程技术网

Python 多处理共享变量(自建类型)

Python 多处理共享变量(自建类型),python,Python,我有一个GUI,我想在其中进行多处理。当我想共享我创建的类中的变量时,我的问题就来了。 我试图通过使用类multiprocessing.Queue[maxsize]将其共享到同时运行的两个模块中。 但它不起作用 sch=sched() q=Queue() q.put([sch]) def foo1(): sch=q.get([sch]) event=Event(8) #another class created sch.add_list(event) q.put([sc

我有一个GUI,我想在其中进行多处理。当我想共享我创建的类中的变量时,我的问题就来了。 我试图通过使用类multiprocessing.Queue[maxsize]将其共享到同时运行的两个模块中。 但它不起作用

sch=sched()
q=Queue()
q.put([sch])

def foo1():
   sch=q.get([sch])
   event=Event(8) #another class created
   sch.add_list(event)
   q.put([sch])
   time.sleep(12)

def foo2():
   time.sleep(4)
   sch=q.get([sch])
   q.put([sch])
   print(sch.event_list)

if __name__ == '__main__':
    p1 = Process(target=foo1)
    p2 = Process(target=foo2)
    p1.start()
    p2.start()

您必须将队列实例作为参数传递给进程

从多处理导入进程,队列 def foo1q: sch=q.get[sch] 事件=事件8 附表添加列表项 q、 put[sch] 时间到了 def foo2q: 时间到了 sch=q.get[sch] q、 put[sch] printsch.event\u列表 如果uuuu name uuuuu==\uuuuuuuu main\uuuuuuuu: q=队列 sch=sched q、 put[sch] p1=Processtarget=foo1,args=q, p2=Processtarget=foo2,args=q, p1.开始 p2.开始 当Python派生一个新进程时,它会得到自己的名称空间,因此不可能从子进程中访问主程序的变量

如果希望共享一个对象而不通过管道或队列传递,还可以使用Python管理器类,请参见。管理器创建可从子进程访问的共享对象。我想,sch是一个列表,所以我在示例中使用了manager.list。对于其他数据类型,只需查看Python文档

从多处理导入进程,管理器 def foo: sch=sch_共享从共享列表读取 如果uuuu name uuuuu==\uuuuuuuu main\uuuuuuuu: sch=sched 经理 sch_shared=mgr.listsch创建共享列表并返回代理 p=Processtarget=foo p、 开始
foo1和foo2没有任何争论,你在传递争论吗?@EkremDİNİEL:谢谢。我忘了添加它。事实上,我只是想在bouth进程上更新这个var,所以我应该选择添加到队列上的最后一项。但我认为这不是最佳方案,但我找不到其他解决方案。你知道吗?谢谢但它不起作用。。。请说得更具体些。