如何从 pool.map() 中的所有进程获取变量值?

时间:2021-06-29 20:45:05

标签: python-3.x multiprocessing python-3.8

我正在创建一个程序,该程序使用一些 USB 摄像头捕获图像,然后在其中找到绿色。我想整齐地打印这些相机的帧速率。要使用多个相机运行我的脚本,我使用了多处理库。然后我使用 pool.map() 为每个相机运行我的更新方法。我无法使用更新方法以可以整齐打印的方式报告 fps。

from multiprocessing import Pool
import time


class testing():
    def __init__(self):
        print("initializing...")

    def start(self):
        pool = Pool()
        pool.map(update, [0,0,0,0,0,0]) # list is required but obsolete

    def update(self, num):
        fps = 0.0 # this is the variable I would like to return!
        count= 0.0
        prev = 0.0

        while True:
            try:
                now = time.time()
                count += 1
                elapsed = now - prev
                if elapsed >= 2.5:  # this will print the framerate every ~2.5 seconds
                    fps = count / (now - prev)
                    print(f"{fps:.1f}")
                    count = 0.0
                    prev = now
            except Exception as e:
                print("An error has occured", e)

testObj = testing()
testObj.start()

带有相机 ID 和 fps 的表格是我想要的打印方式。这样的事情...

         -------------------------
        | camera 0   |   FPS: ... |
        | camera 1   |   FPS: ... |
        | ...                     |
         -------------------------

我想用新的 fps 值不断更新表格。我曾尝试使用 yield 来返回值,但我收到一个错误,因为我无法pickle 生成器对象。如果我使用 return 它会停止我需要的 while 循环。

目前,我在 update 方法中放置了一个打印语句,该语句仅显示它正在使用的相机的 fps。所以当我连接 3 或 4 个摄像头时,它变得更难阅读。目标是在一个打印语句或类似的东西中打印所有相机的 FPS。

0 个答案:

没有答案