Search code examples
pythonmultithreadingschedule

How can I run a thread in a scheduled job, outside of "__main__"?


I want to do something like that:

def foo(tickers):
    executor = ProcessPoolExecutor(2)
    loop = asyncio.get_event_loop()

    for ticker in tickers:
        loop.run_in_executor(executor, get_data,ticker)


schedule.every().minute.at(":00").do(foo)

while True:
    schedule.run_pending()

I'm using the "schedule" package to execute a function foo at a precise time of the minute. But right now, I can only execute the threaded function with:

if __name__ == "__main__":
        foo(tickers)

How can I have it executed every minute with that schedule object?

EDIT: Here is my modified code:

executor = ProcessPoolExecutor(2)

def foo(tickers):
    args = (ticker for ticker in tickers)
    executor.map(get_data,args)

if __name__ == "__main__":
    foo(tickers)

Solution

  • See my comment to your question. Here is an example of what I mean using the built-in sched.scheduler class. But you can the schedule module instead with little change:

    import multiprocessing
    from sched import scheduler
    
    def worker(x):
        print(x, flush=True)
    
    def foo():
        pool.map(worker, (1,2))
    
    def main():
        global pool
        import time
    
        pool = multiprocessing.Pool(2)
        s = scheduler(time.time, time.sleep)
        s.enter(delay=1, priority=1, action=foo)
        s.enter(delay=2, priority=1, action=foo)
        s.run()
    
    if __name__ == '__main__':
        main()