在Python过程之间进行交流

发布于 2025-02-04 16:37:48 字数 1059 浏览 4 评论 0原文

上下文

  • 使用
  • ” ),以及游戏的每个实例的一个驱动程序(4-8个实例),
  • 我需要在控制器和驱动程序
  • 速度之间发送数据(指令和状态)是关键,请考虑30-60 fps,因此我们需要并行运行

问题:当我尝试使用多处理时,在内部带有VgamePad对象的驱动程序对象上,我会得到以下错误attributeError:contery cackle cckle local对象'cfunctype'cfunctype'cfunctype。 cfunctionType'。因此,我无法使用多处理之间在过程之间共享VgamePad对象。请参阅下面的MRE:

import vgamepad as vg
from multiprocessing import Pool

class Driver:
    def __init__(self):
        self.gamepad = vg.VX360Gamepad()
drivers = [Driver() for _ in range(3)]

with Pool() as pool:
    rst = pool.starmap(Driver, drivers)

>>> AttributeError: Can't pickle local object 'CFUNCTYPE.<locals>.CFunctionType'

解决方案

  • 运行单独的Python进程,一个用于控制器的过程,许多用于驱动程序的过程,然后配置一个消息传递API以共享它们之间的数据。例如,驾驶员将发送游戏的屏幕截图,并且控制器将发送有关该怎么做的说明。对于消息传递,我在想:
    • 泡菜并仅加载数据
    • 快速API

问题:在驱动程序和控制器之间共享此信息的最佳方法是什么?

  • 对更好的整体体系结构开放,允许将所有数据保存在内存中。

Context:

  • Controlling a game using vgamepad
  • I have a controller (the brains), and one driver for each instance of the game (4-8 instances)
  • I need to send data (instructions and states) between the controller and the drivers
  • Speed is key, think 30-60 fps, so we need to run in parallel

Problem: When I try to use multiprocessing, on the driver object with a vgamepad object inside, I get the following error AttributeError: Can't pickle local object 'CFUNCTYPE.<locals>.CFunctionType'. So I can't share the vgamepad objects between processes using multiprocessing. See MRE below:

import vgamepad as vg
from multiprocessing import Pool

class Driver:
    def __init__(self):
        self.gamepad = vg.VX360Gamepad()
drivers = [Driver() for _ in range(3)]

with Pool() as pool:
    rst = pool.starmap(Driver, drivers)

>>> AttributeError: Can't pickle local object 'CFUNCTYPE.<locals>.CFunctionType'

Solution:

  • Run seperate Python processes, one for the controller, and many for the drivers then configure a messaging API to share data between them. For example, the drivers would send a screenshot of their game, and the controller would send instructions on what to do. For messaging, I was thinking:
    • Pickle and load just the data
    • Fast API

Question: What is the best way to share this information between a driver and controller?

  • Open to better overall architectures that allow all the data to be kept in memory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文