简体   繁体   English

Python:在没有GIL的情况下绘制一些数据(matplotlib)

[英]Python: Plot some data (matplotlib) without GIL

my problem is the GIL of course. 我的问题当然是GIL。 While I'm analysing data it would be nice to present some plots in between (so it's not too boring waiting for results) 在我分析数据时,最好在两者之间展示一些情节(所以等待结果并不是太无聊)

But the GIL prevents this (and this is bringing me to the point of asking myself if Python was such a good idea in the first place). 但GIL阻止了这一点(这让我开始问自己,Python本来就是一个好主意)。

I can only display the plot, wait till the user closes it and commence calculations after that. 我只能显示情节,等到用户关闭它并在此之后开始计算。 A waste of time obviously. 显然浪费时间。

I already tried the subprocess and multiprocessing modules but can't seem to get them to work. 我已经尝试过子进程和多处理模块,但似乎无法让它们工作。

Any thoughts on this one? 对这个有什么想法吗? Thanks 谢谢

Edit: Ok so it's not the GIL but show(). 编辑:好的,所以它不是GIL而是show()。

This is not a problem from matplotlib or the GIL. 这不是来自matplotlib或GIL的问题。

In matplotlib You can open as many figures as you want and have them in the screen while your application continues doing other things. 在matplotlib中您可以打开任意数量的图形,并在应用程序继续执行其他操作时将它们放在屏幕中。

You must use matplotlib in interactive mode. 您必须在交互模式下使用matplotlib。 This probably is your problem. 这可能是你的问题。

from matplotlib import interactive
interactive(True)

this should be at the top of your imports 这应该是你的进口的顶部

This has nothing to do with the GIL, just modify your analysis code to make it update the graph from time to time (for example every N iterations). 这与GIL无关,只需修改分析代码,使其不时更新图形(例如每N次迭代)。

Only then if you see that drawing the graph slows the analysis code too much, put the graph update code in a subprocess with multiprocessing. 只有这样,如果您看到绘制图形会使分析代码过于缓慢,请将图形更新代码放在具有多处理功能的子流程中。

I think you'll need to put the graph into a proper Windowing system, rather than relying on the built-in show code. 我认为您需要将图形放入适当的Windowing系统,而不是依赖于内置的show代码。

Maybe sticking the .show() in another thread would be sufficient? 也许在另一个线程中坚持使用.show()就足够了?

The GIL is irrelevant - you've got a blocking show() call, so you need to handle that first. GIL是无关紧要的 - 你有一个阻塞的show()调用,所以你需要先处理它。

It seems like the draw() method can circumvent the need for show(). 似乎draw()方法可以避免使用show()。

The only reason left for .show() in the script is to let it do the blocking part so that the images don't disapear when the script reaches its end. 在脚本中留下.show()的唯一原因是让它执行阻塞部分,以便在脚本到达结尾时图像不会消失。

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

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