简体   繁体   English

多处理队列:Pygame表面死亡

[英]Multiprocessing queue: pygame surface died

I want to use multiprocessing to separate two tasks in pygame: Pulling an image from a webcam and showing the image on a screen. 我想在pygame中使用多重处理来分离两个任务:从网络摄像头提取图像并将其显示在屏幕上。 The reason I want to do this, is because 我想这样做的原因是

  • I want to have fancy image processing 我想进行精美的图像处理
  • I want to separate the web cam polling from the screen and user input 我想将网络摄像头轮询与屏幕和用户输入分开

and want to separate the time delay of both, ideally optmizing the load. 并希望将两者的时间延迟分开,从而理想地优化负载。 Using threading I have no issues sending images from one thread to the other. 使用线程我没有问题将图像从一个线程发送到另一个线程。 However, I get a laggy image. 但是,我得到的图像比较滞后。 I would like to test if using multiprocessing can reduce delay of the image. 我想测试使用多重处理是否可以减少图像延迟。

Here's the snag: I get an error after sending the image from the camera process to the screen process. 这是一个障碍:将图像从摄像头过程发送到屏幕过程后,出现错误。 After pulling the image from the queue using 从队列中提取图像后

imgmsg = img_q.get() 

I check the image for size with 我检查图像的大小

imgmsg.img.get_width()

As said, with threads this returns the correct image width. 如前所述,使用线程返回正确的图像宽度。 However with multiprocessing I get the following error 但是,通过多处理我得到以下错误

Process Process-1: Traceback (most recent call last):   File "/usr/lib/python3.2/multiprocessing/process.py", line 267, in
_bootstrap
    self.run()   File "/usr/lib/python3.2/multiprocessing/process.py", line 116, in run
    self._target(*self._args, **self._kwargs)   File "test_photoBoothMultiProc.py", line 21, in consumer
    photoBoothScreen.screenThread(in_q, img_q)   File "/home/pi/pyBooth/thread_photoBoothScreen.py", line 68, in screenThread
    print(imgmsg.img.get_width()) pygame.error: display Surface quit

So it seems the image is lost in the queue? 这样看来图像在队列中丢失了吗? I have tried to read into this, and there seem to problems when trans mitting larger objects through a queue. 我试图读懂这一点,当通过队列传输较大的对象时似乎出现了问题。 Is this correct? 这个对吗? How would I circumvent or fix this? 我将如何规避或解决此问题?

Serialize the images first (with pygame.image.tostring / pygame.image.fromstring ) before sending to another process. 首先将图像序列化(使用pygame.image.tostring / pygame.image.fromstring ),然后再发送到另一个进程。

That should work. 那应该工作。 This way, you send only the data of the image itself, not a Surface instance (so it's totally independent from pygame). 这样,您只发送图像本身的数据,而不发送Surface实例(因此它完全独立于pygame)。

Note that you could also compress the string further with simply calling .encode("zlib") / .decode("zlib") on that string. 请注意,您还可以通过简单地对该字符串调用.encode("zlib") / .decode("zlib")来进一步压缩该字符串。

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

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