gpt4 book ai didi

Python 多线程(while 和 apscheduler)

转载 作者:行者123 更新时间:2023-12-01 05:20:00 24 4
gpt4 key购买 nike

我试图在 Python 中同时调用两个函数。一种是无限循环,另一种是使用 apscheduler 启动。像这样:

Thread.py

from multiprocessing import Process
import _While
import _Scheduler

if __name__ == '__main__':
p1 = Process(target=_While.main())
p1.start()
p2 = Process(target=_Scheduler.main())
p2.start()

_While.py

import time


def main():
while True:
print "while"
time.sleep(0.5)

_Scheduler.py

import logging
from apscheduler.scheduler import Scheduler


def _scheduler():
print "scheduler"


if __name__ == '__main__':
logging.basicConfig()
scheduler = Scheduler(standalone=True)
scheduler.add_interval_job(lambda: _scheduler(), seconds=2)
scheduler.start()

由于仅打印了 while ,因此 _Scheduler 似乎尚未启动。有人可以帮助我吗?

最佳答案

这里至少有几个问题。首先,target 关键字应该是函数,而不是函数的结果。例如:

p1 = Process(target=_While.main)  # Note the lack of function call

其次,我没有看到任何 _Scheduler.main 函数。也许您打算做类似的事情:

import logging
from apscheduler.scheduler import Scheduler


def _scheduler():
print "scheduler"


def main():
logging.basicConfig()
scheduler = Scheduler(standalone=True)
scheduler.add_interval_job(_scheduler, seconds=2) # I doubt that `lambda` is necessary here ...
scheduler.start()

if __name__ == "__main__":
main()

关于Python 多线程(while 和 apscheduler),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22592092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com