暂停程序 - 轮询/睡眠/线程? [英] Pausing a program - poll/sleep/threads?

查看:69
本文介绍了暂停程序 - 轮询/睡眠/线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为XMMS编写一个PyQt网络客户端,使用InetCtrl插件,

在连接上获得一个跟踪长度。


要保存在带宽方面,我不想继续查询

服务器的更新(例如当前的音轨已经完成了吗?)所以我认为
是最好的选择在轨道长度为
到期之后才做更新。


那么,我如何让Python程序自动调用函数
$ b预设一段时间后$ b,没有Python进程运行前景(实际上是单任务)?


我认为睡眠()会在前台工作(有点像一段时间

循环,我遇到过wxPython的问题!)Python有没有?b $ b任何轮询功能?


线程怎么样 - 如果我发起一个线程只是等待直到

时间到期,我才可以获得该主线程序来进行主程序

调用一个函数,还是主程序会坐在那里等待

线程结束?我真的没有Python的线程经验,任何

提示/教程,文档相当有限?


我想制作这个程序跨平台,它必须运行至少
至少Windows和Linux(希望也是MacOSX)所以解决方案将是b / b
必须是可移植的(Windows甚至没有线程) POSIX?)


如果你想看看我到目前为止做了什么(GUI,跟踪信息,

控制等)你''我需要XMMS, http://inetctrl.sourceforge.net 和我的
$来自 http://www.the-jedi.co的b $ b来源.uk / downloads / xmmsclient 还有

一些用Cygwin / KDE端口构建的Windows二进制文件。


[InetCtrl似乎如果您没有加载播放列表,则会出现段错误,所以

首先加载播放列表,然后启用一般插件。我必须向作者提交一份

错误报告,也许我可以说服他在

a跟踪结束时播出...]


任何帮助都很棒,因为播放列表支持和这些定时

更新关于所有需要做的事情......

I''m writing a PyQt network client for XMMS, using the InetCtrl plugin,
that on connection receives a track length.

To save on bandwidth, I don''t want to be continually querying the
server for updates (e.g. has the current track finished yet?) so I
figured the best thing to do is just update after the track length has
expired.

So, how would I make a Python program automatically call a function
after a preset period of time, without the Python process running in
the foreground (effectively single-tasking)?

I think that sleep() would work in the foreground (kind of like a while
loop does, I''ve come across that issue with wxPython!) does Python have
any polling capabilities?

What about threading - if I launched a thread to just "wait" until a
time has expired, could I then get that thread to make the main program
call a function, or would the main program sit there waiting for the
thread to end? I really have no experience with threads in Python, any
hints/tutorials, the docs are pretty limited?

I''m looking to make this program cross-platform, it has to run on at
least Windows and Linux (hopefully MacOSX too) so the solution would
have to be portable (does Windows even have threading without POSIX?)

If you want to check out what I''ve done so far (GUI, track info,
controls etc.) you''ll need XMMS, http://inetctrl.sourceforge.net and my
source from http://www.the-jedi.co.uk/downloads/xmmsclient there''s also
some Windows binaries built with the Cygwin/KDE port.

[InetCtrl seems to segfault if you don''t have a playlist loaded, so
load a playlist first, then enable the general plugin. I must submit a
bug report to the author, maybe I could convince him to broadcast when
a track ends...]

Any help would be excellent, as playlist support and these "timed
updates" are about all that''s left needing to be done....

推荐答案

" Simon John" < SI ************ @ yahoo.co.uk>写道:
"Simon John" <si************@yahoo.co.uk> writes:
那么,如何让Python程序在预设的一段时间后自动调用一个函数,而不会在前景中运行Python进程(实际上是单个 - 任务)?
So, how would I make a Python program automatically call a function
after a preset period of time, without the Python process running in
the foreground (effectively single-tasking)?




查看信号模块并使用报警信号。



See the signal module and use the alarm signal.


导入时间


play_something()


time.sleep(LengthOfSongInSeconds)


do_something()

你试过吗?看到你有这个应用我会很有趣。 !

import time

play_something()

time.sleep(LengthOfSongInSeconds)

do_something()
Have you tried that? I''d be interesting in seeing this app you have. !


我不认为time.sleep()会运行得太好,我认为这会导致

程序在前台闲逛,并阻止GUI

更新。


我会试一试,以确保,因为我可以找不到

信号/警报的事情(当我点击

按钮时,警报似乎只会触发,而不是在n秒后......

I don''t think time.sleep() will work too well, I think it will cause
the program to hang around in the foreground, and prevent the GUI
updating.

I''ll give it a try just to make sure, as I can''t figure out the
signal/alarm thing (the alarm only seems to trigger when I click a
button, not after n-seconds....


这篇关于暂停程序 - 轮询/睡眠/线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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