简体   繁体   English

从 Python 中的多个正在运行的进程更新 Tkinter

[英]Updating Tkinter from multiple running Processes in Python

I have three processes running concurrently.我有三个同时运行的进程。 These Processes receive data at microsecond interval with the help of Queue.这些进程在队列的帮助下以微秒间隔接收数据。 The processes are never joined and basically live in a while loop till program is terminated.这些进程永远不会加入,并且基本上处于一个while循环中,直到程序终止。 Calculations on the received data are made and if certain conditions are met, a variable needs to be sent back to the gui.对接收到的数据进行计算,如果满足某些条件,则需要将变量发送回 gui。 All three processes use the same functions.所有三个进程都使用相同的功能。

class Calc:

    def __init__(self):
        self.x = [0,0,0,0,0]
        self.y = [0,0,0,0,0]

    def doCalculations(self, x, y, i):
        #do calculation stuff.
        #if certain conditions are met:
        #self.returnFunction(i)

    def returnFunction(self, i):
        return (self.x[i] - self.y[i]), i # These are two integers

def startProcesses(q: multiprocessing.Queue):
    calc = Calc()
    while True:
        x, y, i = q.get()
        calc.doCalculations(x,y,i)

def main():
    q1 = Queue()
    q2 = Queue()
    q3 = Queue()
    p1 = Process(target=startProcesses, args=(q1,))
    p2 = Process(target=startProcesses, args=(q2,))
    p3 = Process(target=startProcesses, args=(q3,))
    p1.start()
    p2.start()
    p3.start()
    #run() which is the structure that feeds the data to the queues.

if __name__ == '__main__':
    main()

Now i want to add the gui, which is fine but how do i sent the data from the process back to the gui?现在我想添加 gui,这很好,但我如何将数据从进程发送回 gui? Do i make another Queue() in the Process itself?我是否在进程本身中创建另一个 Queue()? This would add 3 more queues because the processes dont share memory.这将增加 3 个队列,因为进程不共享内存。 Or is there a more elegant/simpler way to do this?还是有更优雅/更简单的方法来做到这一点?

You just need to pass one queue if all you want is the result.如果你想要的只是结果,你只需要传递一个队列。 Create one queue and pass it to all three processes.创建一个队列并将其传递给所有三个进程。 They can then add results on the queue itself while you can receive them from the parent process.然后,他们可以在队列本身上添加结果,而您可以从父进程接收它们。

However, if you want to identify which results came from which process, and only get those specific results, then you can use a managed dictionary ( reference )但是,如果您想确定哪些结果来自哪个进程,并且只获得那些特定的结果,那么您可以使用托管字典( 参考

For example, create a dictionary with keys p1 , p2 and p3 .例如,使用键p1p2p3创建一个字典。 These will be where each process will store their results.这些将是每个进程存储其结果的地方。 Then pass this dictionary to each process.然后将这个字典传递给每个进程。 When the process wants to return something, make it edit the value inside the relevant key (process 1 edits p1 , etc., you can pass an additional string to each process which points to the key it should edit).当进程想要返回某些东西时,让它编辑相关键中的值(进程 1 编辑p1等,您可以向每个进程传递一个额外的字符串,该字符串指向它应该编辑的键)。 Since this dictionary is synchronized across processes, these values will be available to the parent process as well.由于此字典跨进程同步,因此这些值也可用于父进程。 This way, you would not have to create three separate structures.这样,您就不必创建三个单独的结构。

To create a managed dictionary:要创建托管字典:

from multiprocessing import Manager

if __name__ == "__main__":
    manager = Manager()
    d = manager.dict({'p1': None, 'p2': None, 'p3': None})

Be sure to close the manager using manager.shutdown() so it gets garbage collected as well.确保使用manager.shutdown()关闭管理器,这样它也会被垃圾收集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM