Wait for db future to complete?

一世执手 提交于 2019-12-13 02:35:41

问题


I have written a code for sanic application, rethinkdb is being used as a backend database. I want to wait for rethinkdb connection function to intialise before other functions as they have dependency on rethinkdb connection.

My rethinkdb connection initialization function is:

async def open_connections(app):
   logger.warning('opening database connection')
   r.set_loop_type('asyncio')
   connection= await r.connect(
       port=app.config.DATABASE["port"],
       host=app.config.DATABASE["ip"],
       db=app.config.DATABASE["dbname"],
       user=app.config.DATABASE["user"],
       password=app.config.DATABASE["password"])
   print (f"connection established {connection}")
   return connection

The call back function which will be executed after future gets resolved is

def db_callback(future):
        exc = future.exception()
        if exc:
            # Handle wonderful empty TimeoutError exception
            logger.error(f"From mnemonic api isnt working with error {exc}")
            sys.exit(1)

        result = future.result()
        return result

sanic app:

def main():
        app = Sanic(__name__)
        load_config(app)
        zmq = ZMQEventLoop()
        asyncio.set_event_loop(zmq)
        server = app.create_server(
            host=app.config.HOST, port=app.config.PORT, debug=app.config.DEBUG, access_log=True)
        loop = asyncio.get_event_loop()

        ##not wait for the server to strat, this will return a future object
        asyncio.ensure_future(server)

        ##not wait for the rethinkdb connection to initialize, this will return
        ##a future object
        future = asyncio.ensure_future(open_connections(app))
        result = future.add_done_callback(db_callback)
        logger.debug(result)

        future = asyncio.ensure_future(insert_mstr_account(app))
        future.add_done_callback(insert_mstr_acc_callback)


        future = asyncio.ensure_future(check_master_accounts(app))
        future.add_done_callback(callbk_check_master_accounts)

        signal(SIGINT, lambda s, f: loop.close())


        try:
            loop.run_forever()
        except KeyboardInterrupt:
            close_connections(app)
            loop.stop()

When i start this app, the print statement in open_connections functions executes in the last.


回答1:


future = asyncio.ensure_future(open_connections(app))
result = future.add_done_callback(db_callback)

ensure_future schedules coroutines concurrently

add_done_callback does not wait for the completion of the future, instead it simply schedules a function call after the future is completed. You can see it here

So you should explicitly await the open_connections future before performing other functions:

future = asyncio.ensure_future(open_connections(app))
future.add_done_callback(db_callback)
result = await future

EDITED: the answer above applies only to coroutine

In this case we want to wait for the completion of future in the function body. To do it we should use loop.run_until_complete

def main():
    ...
    future = asyncio.ensure_future(open_connections(app))
    future.add_done_callback(db_callback)
    result = loop.run_until_complete(future)


来源:https://stackoverflow.com/questions/52526506/wait-for-db-future-to-complete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!