Implementing pyqtgraph multiprocessing into a pyqt widget

前端 未结 1 818
有刺的猬
有刺的猬 2021-01-03 08:55

I am trying to plot images on a GUI that I am designing in Python. The full program will be collecting image data from a camera and then displaying the images on the GUI. I

相关标签:
1条回答
  • 2021-01-03 09:06

    There are three general approaches to keeping your GUI responsive while doing work in the background:

    1. Timers -- Everything operates within the GUI thread and your work is done by having a timer call some function in between GUI updates. This is the simplest and most common approach and is very likely to be adequate (if written correctly) using either pyqtgraph or matplotlib. The drawback of this approach is that if the work function takes a long time, the GUI will become unresponsive. Pulling data from a camera and displaying it should not cause you problems, though.
    2. Threads -- In the case that your work function becomes too long, you can move some of the work out to a separate thread. Importantly, you can NOT make changes to the GUI from a different thread. Do as much work as possible, then send the results to the GUI thread to be displayed.
    3. Processes -- This is more or less the same approach as using threads, but has the advantage that you may get better CPU utilization (read about the global interpreter lock in python), and the disadvantage that communication between processes can be more cumbersome than between threads. Pyqtgraph has built-in multiprocessing features to make this easier (see pyqtgraph/examples/RemoteSpeedTest.py)

    This topic is discussed extensively elsewhere, so I'll leave it at that.

    With that in mind, there are a few problems with the code you have posted:

    1. You are generating and displaying a 3D data set (100-frame video) with every frame update. This is the reason it appears to update so slowly. See pyqtgraph/examples/ImageItem.py and VideoSpeedTest.py for examples of video done correctly.
    2. You are calling setImage from the worker thread; this may work sometimes, but expect it to crash. A better approach is to generate the data in the worker, then send the data to the main GUI thread to be displayed. The Qt documentation discusses threading in depth. (but as I mentioned before, avoiding threads altogether would be even better)

    Finally, one important rule to follow: If you are having performance issues, profile your code. You cannot fix the problem until you know what is causing it.

    0 讨论(0)
提交回复
热议问题