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

发布于 2024-08-30 12:20:04 字数 231 浏览 4 评论 0原文

我的问题当然是GIL。当我分析数据时,最好在中间呈现一些图(这样等待结果就不会太无聊)

但是 GIL 阻止了这种情况(这让我问自己 Python 是否是一个好主意首先)。

我只能显示绘图,等到用户关闭它并开始计算。显然是浪费时间。

我已经尝试过子进程和多处理模块,但似乎无法让它们工作。

对这个有什么想法吗? 谢谢

编辑:好的,所以这不是 GIL,而是 show()。

my problem is the GIL of course. 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).

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().

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

○闲身 2024-09-06 12:20:04

这不是 matplotlib 或 GIL 的问题。

在 matplotlib 中,您可以打开任意数量的图形,并将它们显示在屏幕上,同时您的应用程序继续执行其他操作。

您必须在交互模式下使用 matplotlib。这可能是你的问题。

from matplotlib import interactive
interactive(True)

这应该位于您导入的顶部

This is not a problem from matplotlib or the 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.

You must use matplotlib in interactive mode. This probably is your problem.

from matplotlib import interactive
interactive(True)

this should be at the top of your imports

在巴黎塔顶看东京樱花 2024-09-06 12:20:04

这与 GIL 无关,只需修改您的分析代码以使其不时更新图(例如每 N 次迭代)。

只有这样,如果您发现绘制图形使分析代码减慢太多,请将图形更新代码放入具有多处理的子进程中。

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).

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.

苹果你个爱泡泡 2024-09-06 12:20:04

我认为您需要将图形放入适当的窗口系统中,而不是依赖于内置的显示代码。

也许将 .show() 粘在另一个线程中就足够了?

GIL 是无关紧要的——你有一个阻塞的 show() 调用,所以你需要先处理它。

I think you'll need to put the graph into a proper Windowing system, rather than relying on the built-in show code.

Maybe sticking the .show() in another thread would be sufficient?

The GIL is irrelevant - you've got a blocking show() call, so you need to handle that first.

小瓶盖 2024-09-06 12:20:04

看起来draw()方法可以绕过show()的需要。

脚本中保留 .show() 的唯一原因是让它执行阻塞部分,以便当脚本到达末尾时图像不会消失。

It seems like the draw() method can circumvent the need for 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文