Skip to content

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

Merged
1st1 merged 2 commits intopython:masterfrom
1st1:c_get_running
Dec 13, 2017
Merged

bpo-32296: Implement asyncio.get_event_loop and _get_running_loop in C.#4827
1st1 merged 2 commits intopython:masterfrom
1st1:c_get_running

Conversation

@1st1
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread Modules/_asynciomodule.c Outdated
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

4 participants