English 中文(简体)
Python 核心应用
原标题:Python core application

I m new to Python and I m writing a script that includes some timed routines. My current approach is to instantiate a class that includes those Timers (from: threading.Timer), but I don t want the script to return when it gets to the end of the function:

import mytimer

timer = mytimer()

Suppose I have a imple script like that one. All it does is instantiate a mytimer object which performs a series of timed activities.

为了不退出申请,我可以这样使用Qt:

from PyQt4.QtCore import QCoreApplication
import mytimer
import sys

def main():
    app = QCoreApplication(sys.argv)
    timer = mytimer()

    sys.exit(app.exec_())

if __name__ ==  __main__ :
    main()

This way, the sys.exit() call won t return immediately, and the timer would just keep doing its thing forever in background.

Although this is a solution I ve used before, using Qt just for this doesn t fell right to me. So my question is, Is there any way to accomplish this using standard Python?

谢谢 谢谢

最佳回答

在您的脚本中创建函数, 测试 < a href=> "http://docs.python.org/library/select.html#poll-objects" rel="nofollow" >select 或民调对象 以终止循环。 请查看标准库中的 serv_forever , 例如 http://svn.python.org/project/python/branches/release25-maint/Lib/SocketServer.py="nofollow"\code>>Socketserver. py 。

问题回答

谷歌搜索“ python 定时器” 发现 :

http://docs.python.org/library/sched.html

http://docs.python.org/release/2.5.2/lib/timer-objects.html

ched 模块似乎是您需要的 。

示例:

>>> import sched, time
>>> s = sched.scheduler(time.time, time.sleep)
>>> def print_time(): print "From print_time", time.time()
...
>>> def print_some_times():
...     print time.time()
...     s.enter(5, 1, print_time, ())
...     s.enter(10, 1, print_time, ())
...     s.run()
...     print time.time()
...
>>> print_some_times()
930343690.257
From print_time 930343695.274
From print_time 930343700.273
930343700.276

一旦您为要发生的事情构建了时间的队列, 您只需在您的 < code>. run () 方式上调用 < code> 方法, 它将自动等待队列被清空, 然后完成 。 这样您就可以将 < code>. run () 作为您脚本中的最后一件事情, 只有当时间任务全部完成时它才会自动退出 。

import mytimer
import sys

from threading import Lock

lock = Lock()
lock.acquire() # put lock into locked state

def main():
    timer = mytimer()

    lock.acquire()  # blocks until someone calls lock.release()

if __name__ ==  __main__ :
    main()

如果您想要一个干净的退出, 您可以在某个时刻只做 mytimer () call lock. release ()





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签