Python Event.wait()占用高CPU
我的程序有6个线程,它们在特定时间后连续运行,并且使用相同的目标Python Event.wait()占用高CPU,python,python-multithreading,Python,Python Multithreading,我的程序有6个线程,它们在特定时间后连续运行,并且使用相同的目标 running = True thread_event = Event() def process(obj, source, *args): global running try: while running: obj(*args) thread_event.wait(get_interval(source)) # get_interval
running = True
thread_event = Event()
def process(obj, source, *args):
global running
try:
while running:
obj(*args)
thread_event.wait(get_interval(source)) # get_interval returns time ranging from 5 seconds to 24 hours based on source
time.sleep(0.1) # Adding this line reduces CPU usage from 90-100% to 5-7%
except Exception:
log.exception("An exception has been raised, closing all processes.")
running = False
# notify all wait(s) so they exit immediately
thread_event.set()
thread_event.clear()
def main():
thread_cdtn = Thread(target=process, name='clean_dead_tokens', args=(clean_dead_tokens, 'clean_dead_tokens')) # One example of thread.
thread_cdtn.start() # Start thread here.
# other threads start here.
原始代码没有时间。睡眠(0.1)
并且使用几乎100%的CPU,我理解这是由于造成的,而true:
。但是event.wait()
不会让线程暂停/休眠吗?
使用
time.sleep(0.1)
使CPU使用率下降到5-7%,但这似乎不好或不正确,因为这可能会影响业务逻辑(可能只影响一点点)。有没有其他方法可以管理CPU使用情况?这将是最好的解释
为了
是否有其他方法可以管理CPU使用
也许这只是睡眠。ahaha:-)@user2357112supportsMonica线程正在主服务器中启动。我不明白你的评论。bleh-我误读了
过程
,以为你在使用多处理
。