Ok to pass loop to another thread?

Hi all

I read this warning in another thread -

“The reality is: passing a loop instance between threads brings you to the land where dragons live. It is dangerous but you might survive.”

I do this in my project, so I thought I had better find out if this is safe.

I have a class which represents an asynchronous database connection. It works by creating a separate thread to act as a db handler. It uses a queue.Queue to send requests to the handler. A request consists of the actual command, a reference to the running loop, and an asyncio.Queue to send results back.

The handler gets the request from the request_queue, executes the command, and places the results, in chunks of 50 rows at a time, on the return_queue using loop.call_soon_threadsafe(return_queue.put_nowait, rows)

The caller awaits the return_queue and retrieves the results as they appear.

I also use an asyncio.Event so that if the caller sends a ‘commit’ or ‘rollback’, the handler can notify the caller when it has finished, using loop.call_soon_threadsafe(wait_event.set).

Is there any problem with this approach?

Thanks

1 Like