Skip to content

Setting up asynchronous code in before_all and before_scenario hooks are synchronous : How to configure SQLAlchemy asynchronous scoped session in behave synchronous hooks? #1074

Closed Answered by jenisys
dcs3spp asked this question in Q&A
Discussion options

You must be logged in to vote

The core problem is the asyncio.current_task() function that is used as scopefunc=asyncio.current_task when creating the Session object(-factory). It can only be executed while running in a coroutine scope because it will call asyncio.get_running_loop().

# PART OF: before_all() hook
Session = async_scoped_session(factory(), scopefunc=asyncio.current_task)

OTHERWISE:
The asyncio development has continued and has provided other functionality, probably inspired by anyio.run() or others.
Nowadays, you probably should :

  • use the asyncio.Runners concept, like: asyncio.run() or asyncio.Runner in the behave-hooks instead of using the event-loop.
  • provide async functionality as coroutine that is ex…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jenisys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants