简体   繁体   English

带进程的 Python Apscheduler(多处理)

[英]Python Apscheduler with processes (multiprocessing)

I am trying to run processes (multiprocessing module) through the Apscheduler module我正在尝试通过 Apscheduler 模块运行进程(多处理模块)

    sched = BackgroundScheduler(executors=executors)
    sched.add_listener(my_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR)
    #sched.add_job(run_processes, trigger='cron', hour='14', minute='06')
    sched.add_job(run_processes) # starts it a once
    sched.start()

run_processes looks like this run_processes 看起来像这样

def run_processes():

print ("starting p1")
print ("starting p2")    
##run_process_1()
##run_process_2()
p1 = Process(target = run_process_1, args = ())
p2 = Process(target = run_process_2, args = ())

p1.start()
p2.start()

print p1, p1.is_alive()
print p2, p2.is_alive()
p1.join()
p2.join()
print ("finished")

When I start run_processes() without the Apscheduler it runs normal.当我在没有 Apscheduler 的情况下启动 run_processes() 时,它运行正常。 When I run it through the Apscheduler the processes seems to start (there are new Python instances), but then seem to wait or hang doing nothing.当我通过 Apscheduler 运行它时,进程似乎开始了(有新的 Python 实例),但随后似乎等待或挂起什么都不做。

Does anybody know of a problem there and possibly how to resolve it?有没有人知道那里有问题以及可能如何解决?

You can try simple-scheduler .你可以试试simple-scheduler It has the capacity to spawn child processes, without any effort.它具有生成子进程的能力,无需任何努力。

from simple_scheduler.recurring import recurring_scheduler
from multiprocessing import Process

def f1():
    print("Hi")
    
def f2():
    print("...")

def run_processes():
    p1 = Process(target = f1)
    p1.start()
    p2 = Process(target = f2)
    p2.start()
    p1.join()
    p2.join()
    print("finished")

recurring_scheduler.add_job(target=run_processes,
                            period_in_seconds=5,
                            job_name="run_processes")
recurring_scheduler.job_summary()
recurring_scheduler.run()
recurring_scheduler.job_summary()

The output is输出是

run_processes [recurring | 5-second(s)] running in 11870
Hi
...
finished
Hi
...
finished
Hi
...
finished

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM