Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can it use async job? #239

Open
ghost opened this issue Aug 10, 2018 · 4 comments
Open

Can it use async job? #239

ghost opened this issue Aug 10, 2018 · 4 comments

Comments

@ghost
Copy link

ghost commented Aug 10, 2018

No description provided.

@ivanovishado
Copy link

Can you please add more details about this?

@oubeichen
Copy link

oubeichen commented Nov 15, 2018

async do_something():
    await some_io_func()

schedule.every().minute.do(do_something)
while True:
     schedule.run_pending()
     wait asyncio.sleep(10)

this will return

RuntimeWarning: coroutine 'do_something' was never awaited

@irl
Copy link

irl commented Nov 28, 2018

You need to have your loop stored in a variable and use a wrapper function to schedule the execution of your co-routines on the loop. It might be necessary to run the scheduler in another thread as in https://github.com/mrhwick/schedule/blob/master/schedule/__init__.py#L63. You'll need to take care to use the threadsafe functions for scheduling the execution of the coroutines.

@irl
Copy link

irl commented Nov 28, 2018

Untested guess at how this might work:

import asyncio                                                                   
import time                                                                      
import schedule                                                                  
from threading import Thread                                                     
                                                                                 
loop = asyncio.new_event_loop()                                                  
                                                                                 
def f(loop):                                                                     
    asyncio.set_event_loop(loop)                                                 
    loop.run_forever()                                                           
                                                                                 
t = Thread(target=f, args=(loop,))                                               
t.start()                                                                        
                                                                                 
def run_async(job_coro):                                                         
    job_task = job_coro()                                                        
    loop.call_soon_threadsafe(asyncio.async, job_task)                           
                                                                                 
async def job():                                                                 
    await asyncio.sleep(1)                                                       
    print('Hello, world!')                                                       
                                                                                 
schedule.every(10).seconds.do(run_async, job)                                    
                                                                                 
while 1:                                                                         
    schedule.run_pending()                                                       
    time.sleep(1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants