替代python的time.sleep() [英] alternative to python's time.sleep()

查看:599
本文介绍了替代python的time.sleep()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行实时数据处理+显示,并且每60秒访问一次我们的数据库.我不想使用time.sleep()每60秒等待一次,因为它使我失去了控制权(即对变量的REPL访问,这不是必需的,但很好),并且冻结了matplotlib图表.

I'm performing realtime data processing + display, and I hit our database every 60 seconds. I'd like to not use time.sleep() for waiting every 60 seconds, as it removes control from me (namely REPL access to variables, which isn't necessary but nice) and freezes matplotlib charts.

还有其他选择吗?理想情况下,最初可以将控制权交给用户,然后在60秒后取消控制权,运行一些代码并更新绘图,然后将控制权交还给用户. (当我说控制时,我指的是REPL控制).

Is there an alternative? Ideally, something that would initially give control to the user, and after 60 seconds, take control away, run some code, and update a plot, then give control back to the user. (When I say control, I mean REPL control).

有什么想法吗?

推荐答案

如果您不需要取消用户控制,则有一种非常简单的方法:创建

If you don't need to take away user control, there's a very easy way to do this: Create a threading.Timer.

您想要做的是获取功能的延续",即time.sleep之后的所有内容,然后将其移动到单独的功能my_function中,然后按以下方式进行调度:

What you want to do is take the "continuation" of the function—that is, everything that would come after the time.sleep—and move it into a separate function my_function, then schedule it like this:

threading.Timer(60, my_function).start()

my_function的末尾,它使用完全相同的代码行来计划一个新的Timer.

And at the end of my_function, it schedules a new Timer with the exact same line of code.

Timer是一个非常笨拙的接口和实现,但是它内置在stdlib中.您可以在ActiveState上找到配方,并在PyPI上找到模块,这些配方可以提供更好的类,例如,在一个线程上运行多个计时器,而不是每个计时器运行一个线程,让您安排重复调用,因此您不必继续重新安排自己的时间,等等.对于每60秒运行一次的操作,我认为Timer可能还可以.

Timer is a pretty clunky interface and implementation, but it's built into the stdlib. You can find recipes on ActiveState and modules on PyPI that provide better classes that, e.g., run multiple timers on one thread instead of a thread per timer, let you schedule recurring calls so you don't have to keep rescheduling yourself, etc. But for something that just runs every 60 seconds, I think you may be OK with Timer.

要记住的一件事:如果后台作业需要处理用户在REPL中处理的任何相同数据,则有可能出现竞争状况.通常在交互式环境中(尤其是在Python中,这要归功于GIL),您可以为用户承担责任而不引起任何比赛.如果没有,则需要某种同步.

One thing to keep in mind: If the background job needs to deal with any of the same data the user is dealing with in the REPL, there is a chance of a race condition. Often in an interactive environment (especially in Python, thanks to the GIL), you can just lay the onus on the user to not cause any races. If not, you'll need some kind of synchronization.

要记住的另一件事:如果您要进行GUI工作,则取决于您所使用的GUI(我相信matplotlib是可配置的,但默认为tkinter?),您可能无法从后台线程更新GUI.

Another thing to keep in mind: If you're trying to do GUI work, depending on the GUI you're using (I believe matplotlib is configurable but defaults to tkinter?), you may not be able to update the GUI from a background thread.

但是在那种情况下,实际上有一个更好的解决方案. GUI程序具有在某个线程或其他线程中运行的事件循环,几乎以往的每个事件循环设计都可以在该线程中安排计时器.对于tkinter,如果您具有root对象的句柄,则只需调用root.after(60000, my_function)而不是threading.Timer(60, my_function).start(),它就会在与GUI相同的线程上运行,而不会浪费任何不必要的资源.

But there's actually a better solution in that case anyway. GUI programs have an event loop that runs in some thread or other, and almost every event loop ever design has a way to schedule a timer in that thread. For tkinter, if you have a handle to the root object, just call root.after(60000, my_function) instead of threading.Timer(60, my_function).start(), and it will run on the same thread as the GUI, and without wasting any unnecessary resources.

这篇关于替代python的time.sleep()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆