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

bpo-32296: Implement asyncio.get_event_loop and _get_running_loop in C. #4827

Merged
merged 2 commits into from Dec 13, 2017

Conversation

1st1
Copy link
Member

@1st1 1st1 commented Dec 12, 2017

asyncio.get_event_loop(), and, subsequently asyncio._get_running_loop()
are one of the most frequently executed functions in asyncio. They also
can't be sped up by third-party event loops like uvloop.

When implemented in C they become 4x faster.

https://bugs.python.org/issue32296

Copy link
Contributor

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding to third-party loops and other extensions: it would be nice to have C API for asyncio. Not sure how to do it: asyncio is shared module, Python C API is not linked with _asyncio.

@@ -88,6 +91,134 @@ class _asyncio.Future "FutureObj *" "&Future_Type"
static PyObject* future_new_iter(PyObject *);
static inline int future_call_schedule_callbacks(FutureObj *);


static int
get_running_loop(PyObject **loop)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure you had a reason for this design but I still cannot figure out why not PyObject* get_running_loop().

The function returns a loop, Py_None if loop not found and NULL on error.
Incref/decref of Py_None is instant and relative rare -- we assume the function is called mostly from coroutines.

The signature looks more native to C Python developer -- almost never API call used pointer for storing result but most of them returns PyObject*.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is not a public CPython API. This is an internal helper function, which either sets a pointer to the currently running event loop or not. It returns a status of that operation as an int result. This is a common pattern in C.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it's a common pattern for helper functions in CPython code base too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@1st1
Copy link
Member Author

1st1 commented Dec 13, 2017

Regarding to third-party loops and other extensions: it would be nice to have C API for asyncio. Not sure how to do it: asyncio is shared module, Python C API is not linked with _asyncio.

Yeah, I agree, I want C API too. Let's think about it after 3.7.

@asvetlov
Copy link
Contributor

But wait.
Pure Python fallbacks are not tested.
Would you add them?

asyncio.get_event_loop(), and, subsequently asyncio._get_running_loop()
are one of the most frequently executed functions in asyncio.  They also
can't be sped up by third-party event loops like uvloop.

When implemented in C they become 4x faster.
@1st1
Copy link
Member Author

1st1 commented Dec 13, 2017

Would you add them?

Good idea. Done.

@1st1 1st1 merged commit a70232f into python:master Dec 13, 2017
@1st1 1st1 deleted the c_get_running branch December 13, 2017 19:49
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.

None yet

4 participants