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

Pyo3 update #70

Merged
merged 2 commits into from
Aug 9, 2021
Merged

Pyo3 update #70

merged 2 commits into from
Aug 9, 2021

Conversation

awestlake87
Copy link
Contributor

Hey, the fix was a bit more complicated than I anticipated. Essentially, since you're using run_forever, there was no way of getting the event loop reference via get_running_loop as opposed to get_event_loop (which will fetch the global event loop). I went ahead and scoped the event loop for your index handler so that it has access to the event loop via task-local data.

I've been improving the docs on 0.14, but this problem probably isn't explicitly addressed. The "Event Loop References and Thread-Awareness" section in the docs may provide some intuition for why 0.14 acts this way.

@awestlake87 awestlake87 mentioned this pull request Aug 8, 2021
@sansyrox
Copy link
Member

sansyrox commented Aug 9, 2021

Thank you @awestlake87 for this! ✨

Do the changes even work with v0.13.* ?

@awestlake87
Copy link
Contributor Author

The 0.14 API is pretty different from 0.13, so it won't work with both. If you need it to work with both 0.13 and 0.14, you can delay this PR and continue using the 0.13 API with #[allow(deprecated)] since the 0.13 conversions work the same in 0.13 and 0.14.

@sansyrox
Copy link
Member

sansyrox commented Aug 9, 2021

@awestlake87 , no no. I was asking as the Cargo.toml was not updated. I will update it and check if the tests pass.

@sansyrox sansyrox merged commit 36e3aa5 into sparckles:pyo3-update Aug 9, 2021
@sansyrox
Copy link
Member

sansyrox commented Aug 9, 2021

@awestlake87 , what does pyo3-asyncio::tokio::scope do?

@awestlake87
Copy link
Contributor Author

It allows the conversions to get a reference to the current event loop via task-local data. I've got a breakdown of its motivation and usage here

@ShadowJonathan
Copy link

ShadowJonathan commented Aug 9, 2021

More specifically;

pyo3_asyncio::<runtime>::scope - Store the event loop in task-local data when executing the given Future.

pyo3_asyncio::<runtime>::scope_local - Store the event loop in task-local data when executing the given !Send Future.

This helps with subsequent calls to work with the event loop (such as acquiring python, or spawning other tasks)

sansyrox pushed a commit that referenced this pull request Aug 10, 2021
sansyrox pushed a commit that referenced this pull request Aug 10, 2021
sansyrox pushed a commit that referenced this pull request Aug 13, 2021
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

Successfully merging this pull request may close these issues.

3 participants