如何避免apscheduler将任务添加到子进程?

1 投票
1 回答
651 浏览
提问于 2025-04-18 18:32

我在我的程序中添加了一个apscheduler的任务,然后我发现我的子进程也有这个任务,结果我的主进程和子进程同时执行这个任务...

主程序是

server = StreamServer((server_ip, server_port), serve_forever, spawn = pool, backlog=100000)

for i in range(process_count - 1):
    Process(target=serve_forever, args=()).start()

我的apscheduler设置是

   scheduler =BackgroundScheduler()
    scheduler.add_job(tick, 'interval', seconds=3)
    scheduler.start()

输出结果是

Tick! The time is: 1409042763.26 pocess is 11428
Tick! The time is: 1409042763.26 pocess is 11431
Tick! The time is: 1409042763.26 pocess is 11424
Tick! The time is: 1409042763.26 pocess is 11430
Tick! The time is: 1409042763.26 pocess is 11426
Tick! The time is: 1409042763.26 pocess is 11380
Tick! The time is: 1409042763.26 pocess is 11425
Tick! The time is: 1409042763.26 pocess is 11429
Tick! The time is: 1409042763.26 pocess is 11432
Tick! The time is: 1409042763.26 pocess is 11433

1 个回答

0

一些建议:

  1. 在创建了子进程之后再启动调度器。
  2. 在子进程中关闭调度器。
  3. 用其他方法创建子进程,而不是直接从主进程分叉。

撰写回答