Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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/8/redis/2.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 调用进程的getter返回None_Python_Python 3.x_Process_Multiprocessing - Fatal编程技术网

Python 调用进程的getter返回None

Python 调用进程的getter返回None,python,python-3.x,process,multiprocessing,Python,Python 3.x,Process,Multiprocessing,我目前正在努力使我的程序使用multiprocessing.Process,我想从我的Process子类中获取一个对象 内主.py: p = DataProcessor() p.start() #later: obj = p.x from multiprocessing import Process class DataProcessor(Process): def __init__(self): #call to super etc self.x

我目前正在努力使我的程序使用multiprocessing.Process,我想从我的Process子类中获取一个对象

.py:

p = DataProcessor()
p.start()
#later:
obj = p.x
from multiprocessing import Process
class DataProcessor(Process):
     def __init__(self):
         #call to super etc
         self.x = None

     def run(self):
         while True:
             if self.x is None:
                 self.x = 5 #normally i set this to an object
内置数据处理器.py:

p = DataProcessor()
p.start()
#later:
obj = p.x
from multiprocessing import Process
class DataProcessor(Process):
     def __init__(self):
         #call to super etc
         self.x = None

     def run(self):
         while True:
             if self.x is None:
                 self.x = 5 #normally i set this to an object
当我现在想在我的main中使用x时,它总是无。
如何在不使用多处理队列的情况下使其工作?
(在我看来,当只处理一个对象一次时,队列既不可读也不有用)

您可以使用多处理管道;) 但说真的,您必须使用进程间通信来在进程之间共享数据。下面是一个从子进程检索
x
的简单示例

from multiprocessing import Process, Queue
import time


class DataProcessor(Process):
     def __init__(self, queue):
         #call to super etc
         Process.__init__(self)
         self.queue = queue
         self.x = None

     def run(self):
         while True:
             if self.x is None:
                 self.x = 5 #normally i set this to an object
                 self.queue.put(self.x)

if __name__ == '__main__':
    queue = Queue()
    p = DataProcessor(queue)
    p.start()
    #later:
    while queue.empty():
        time.sleep(.1)
    x = queue.get()
    print(x)

主循环中的
while
循环是否必要?我以为queue.get会做类似的事情,你说得对。我最近在管道方面做得更多。