Search code examples
pythonpython-3.xwhile-loopschedulermultiprocess

In Python run scheduled event function and while loop function with multiprocess with pool


I have two functions. One is celen() which checks the calendar and make schedule to execute something and another one is infinite while loop, tech(). I tried to run by multi-process, couldn't see anything printing on shell and ended up doing following code which at least showing the first process's output.

But, while the first process/ the calendar event with apsscheduler running it shows the all the pending jobs, the second job/function, the infinite loop doesn't start!

How can I run both with multiprocess/subprocess/multithreading while I can still see the output in shell or anywhere from both function?

def trade():
      return(calen(),tech())

with Pool(cpu_count()) as p:
      results = p.map(trade())
      print(list(results))

Previously I also did try

if __name__ == '__main__':
    with Pool(processes=2) as pool:
        r1 = pool.apply_async(calen, ())
        r2 = pool.apply_async(tech, ())

        print(r1.get(timeout=120))
        print(r2.get(timeout=120))

I will appreciate if anyone can give a solve how to run while loop & scheduled event together while outputs are visible.


Solution

  • I guess I am doing mistake with apscheduler. Apschduler it self run multiprocess with schdule and also in interval/while loop.

    The while loop should be executed from apscheduler, not as separate function.

    Instead I trid to do as seperate, one with apsscheduler & another ordinary while loop. WHile apscheduler started it was blocking any other operation.

    This helped me https://devcenter.heroku.com/articles/clock-processes-python

    It's actually good solution for multiprocess as well (as far I have understood)

    from apscheduler.schedulers.blocking import BlockingScheduler
    sched = BlockingScheduler()
    
    @sched.scheduled_job('interval', minutes=3)
    def timed_job():
        print('This job is run every three minutes.')
    
    @sched.scheduled_job('cron', day_of_week='mon-fri', hour=17)
    def scheduled_job():
        print('This job is run every weekday at 5pm.')
    
    sched.start()