[英]Python forking inside multithreading
I want to have a single server and a function to check for all computers in a network. 我希望有一个服务器和一个功能来检查网络中的所有计算机。 I want to call the check for computers function on a regular basis with a fixed time period.
我想在固定的时间段内定期调用计算机功能检查。 So I would like to use 'select' to include the server and the function.
所以我想使用'select'来包含服务器和功能。 Is it allowed to pass a function into select as a parameter or am I only allowed to pass lists into select?
是否允许将一个函数传递给select作为参数,或者我只允许将列表传递给select?
What do you suggest I do to have a function called every 5sec or so? 你有什么建议我每5秒左右调用一次函数?
Try implementing your own thread that calls a passed function every five seconds. 尝试实现自己的线程,每隔五秒调用一次传递函数。
from threading import *
import time
def my_function():
print 'Running ...' # replace
class EventSchedule(Thread):
def __init__(self, function):
self.running = False
self.function = function
super(EventSchedule, self).__init__()
def start(self):
self.running = True
super(EventSchedule, self).start()
def run(self):
while self.running:
self.function() # call function
time.sleep(5) # wait 5 secs
def stop(self):
self.running = False
thread = EventSchedule(my_function) # pass function
thread.start() # start thread
Simply use threading.Timer
只需使用
threading.Timer
import threading
def check_func():
# other stuff
threading.Timer(5.0, check_func).start() # run check_func after 5s
check_func()
This is the standard way to do periodic job in python. 这是在python中执行定期作业的标准方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.