from datetime import datetime
import os
from twisted.internet import reactor
from apscheduler.schedulers.twisted import TwistedScheduler
def tick():
print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
scheduler = TwistedScheduler()
scheduler.add_job(tick, 'interval', seconds=3)
scheduler.start()
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
# Execution will block here until Ctrl+C (Ctrl+Break on Windows) is pressed.
try:
reactor.run()
except (KeyboardInterrupt, SystemExit):
pass
twisted 做为服务器端tcp并发经常需要一些定时器来处理一些事,一般情况下,我们可以使用CallLater只能做到定时器的功能,但是使用任务调度框架就可以实现一些常规化的工作, 如定时产生twisted 当前连接客户端的信息