我有两个线程接收异步数据,并希望在主线程中处理这些数据。主线程调用(ProcessA)运行的第一个进程,但第二个进程从未执行。为了便于空间和解释,我简化了线程。 第一个帖子是:
import logging
import time
logging.basicConfig(filename='ProcessA.log',level=logging.DEBUG)
from multiprocessing import Queue
q = Queue()
class ProcessA(q):
global variableA
logging.info('ProcessA started')
while (True):
variableA = 'A' + str(time.time())
q.put(variableA)
logging.info (variableA)
time.sleep(5)
第二个线程类似:
import time
from multiprocessing import Queue
import logging
logging.basicConfig(filename='ProcessB.log',level=logging.DEBUG)
q = Queue()
class ProcessB(q):
logging.info('ProcessB started')
global variableB
while (True):
variableB = 'B' + str(time.time())
q.put (variableB)
logging.info (variableB)
time.sleep(2)
主要调用线程如下:
import time
from multiprocessing import Process, Queue
import ProcessA.py
import ProcessB.py
import logging
logging.basicConfig(filename='MThread.log',level=logging.DEBUG)
if __name__ == "__main__":
queue = Queue()
a =Process(target=ProcessA, args=(queue,))
a.start()
b = Process(target=ProcessB, args=(queue,))
b.start()
while (True):
if not queue.empty():
variableC = queue.get()
logging.info ("variableC ="+ variableC)
time.sleep(1)
当我运行程序时,只运行ProcessA(正如我在日志文件中看到的那样)。
无论我是在Windows 7终端中运行代码本机还是在Spyder中运行'运行>配置...>,我都会得到相同的结果。在外部终端执行'设置
如何才能让ProcessB和主线程的剩余部分运行?
UPDATE 将类从外部文件移动到内部函数是有效的,但是使用外部函数并不起作用。
这就是这段代码的作用:
import time
from multiprocessing import Process, Queue
import logging
logging.basicConfig(filename='MThread2.log',level=logging.DEBUG)
def ProcessA(q):
global variableA
logging.info('ProcessA started')
while (True):
variableA = 'A' + str(time.time())
q.put(variableA)
logging.info (variableA)
time.sleep(5)
def ProcessB(q):
logging.info('ProcessB started')
global variableB
while (True):
variableB = 'B' + str(time.time())
q.put (variableB)
logging.info (variableB)
time.sleep(2)
if __name__ == "__main__":
queueA = Queue()
a =Process(target=ProcessA, args=(queueA,))
a.start()
queueB = Queue()
b = Process(target=ProcessB, args=(queueB,))
b.start()
while (True):
if not queueA.empty():
variableC = queueA.get()
logging.info ("variableC ="+ variableC)
if not queueB.empty():
variableC = queueB.get()
logging.info ("variableC ="+ variableC)
time.sleep(1)
然而,将代码移动到外部函数中仍然无法正常工作。
答案 0 :(得分:0)
很难确切地说出这里有什么问题。我在导入和分配你的课程时看到了一些小错误......
让您走上正确的道路:
ProcessA和B类可能只是将Queue作为参数的函数,如果你想这样做的话。否则,它应该继承Process(而不是q)并在初始化时占用队列。这是一个很好的演练(队列和管道大约有一半):https://www.geeksforgeeks.org/multiprocessing-python-set-2/
您也可以尝试使用经理分享此状态。见https://docs.python.org/3/library/multiprocessing.html#sharing-state-between-processes
答案 1 :(得分:0)
问题在于我导入外部函数的方式。我不需要' .py'延期。我在这篇文章中发现了这一点:Call a function from another file in Python
以下代码有效:
import time
from multiprocessing import Process, Queue
from ProcessAF import processA
from ProcessBF import processB
import logging
logging.basicConfig(filename='MThread.log',level=logging.DEBUG)
if __name__ == "__main__":
queueA = Queue()
a =Process(target=processA, args=(queueA,))
a.start()
queueB = Queue()
b = Process(target=processB, args=(queueB,))
b.start()
while (True):
if not queueA.empty():
variableC = queueA.get()
logging.info ("variableC ="+ variableC)
if not queueB.empty():
variableC = queueB.get()
logging.info ("variableC ="+ variableC)
time.sleep(1)
在名为ProcessAF.py的文件中的函数位于同一目录中,如下所示:
import logging
import time
logging.basicConfig(filename='ProcessA.log',level=logging.DEBUG)
from multiprocessing import Queue
q = Queue()
def processA(q):
global variableA
logging.info('ProcessA started')
while (True):
variableA = 'A' + str(time.time())
q.put(variableA)
logging.info (variableA)
time.sleep(5)