classification
Title: asyncio: add BaseEventLoop._current_handle (only used in debug mode)
Type: Stage:
Components: asyncio Versions: Python 3.5, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, python-dev, vstinner, yselivanov
Priority: normal Keywords: patch

Created on 2015-01-09 15:34 by vstinner, last changed 2015-01-27 23:34 by python-dev. This issue is now closed.

Files
File name Uploaded Description Edit
current_handle.patch vstinner, 2015-01-09 15:34 review
Messages (7)
msg233759 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-01-09 15:34
One pain point of asynchronous programming is to understand bugs and rebuild the chain of callbacks / coroutines / tasks. I added the source traceback to Handle, Future (Task) and CoroWrapper to help debugging.

Here is a new enhancement to provide more context in debug mode: add BaseEventLoop._current_handle which is the handle currently executed.

The first usage is the call_exception_handler() which logs the source traceback of the current handle in debug mode.

Example:
---
import asyncio

def bug():
    loop.call_exception_handler({'message': 'bug!'})

def schedule_bug():
    bug()

loop = asyncio.get_event_loop()
loop.call_soon(schedule_bug)
loop.call_later(1, loop.stop)
loop.run_forever()
loop.close()
---

Output in debug mode, without the patch:
---
bug!
---

Output in debug mode, with the patch:
---
bug!
handle_traceback: Handle created at (most recent call last):
  File "x.py", line 10, in <module>
    loop.call_soon(schedule_bug)
---

Later, I plan to use the source traceback of the current handle in more places. For example, use it to log messages.

I would like to know "who" logged the "SSL handshake failed". At the beginning, I wanted to add a source traceback to all transports, but it looks simpler to get the source traceback of the current handler. Moreover, this traceback is more useful than the source traceback of the transport.

Previous try to add the source traceback to transports:
https://code.google.com/p/tulip/issues/detail?id=212
msg233760 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-01-09 15:41
Yury Selivanov proposed something different in the past: add a "context" (or a context identifier) to tasks to be able to (indirectly) attach local variables to tasks.

"Add notion of context_id to event loop"
https://code.google.com/p/tulip/issues/detail?id=165

I don't know if BaseEventLoop._current_handle is too specific or might be implemented with a task context. The task context looks to be specific to tasks, whereas handles are very generic in asyncio: almost all functions in asyncio are called in the context of a handle.

Previous discussion related to task context:

"local context in event loop"
https://groups.google.com/forum/#!topic/python-tulip/zix5HQxtElg

"ThreadLocal analogue"
https://groups.google.com/forum/#!topic/python-tulip/j0cSjUGx8qk

See also the tasklocals project:
https://github.com/vkryachko/tasklocals
msg234395 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-01-20 21:40
@Guido, @Yury: What do you think of this feature? Does it make sense to expose (internally) the "handle currently executed"?
msg234396 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2015-01-20 21:51
> What do you think of this feature? Does it make sense to expose (internally) the "handle currently executed"?

I think it's OK to have something like `loop._current_handle` to work ~only~ in debug mode. Enhancing `loop.call_exception_handler` to use it also makes sense. I would also want to make sure, that this property exists only in debug mode and shouldn't be used outside of asyncio.
msg234728 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-01-26 10:07
New changeset 54d74f954bf9 by Victor Stinner in branch '3.4':
Issue #23208, asyncio: Add BaseEventLoop._current_handle
https://hg.python.org/cpython/rev/54d74f954bf9
msg234745 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-01-26 14:06
I commited  current_handle.patch. It's only a first step, I will also change the logger or calls to the logger to use this traceback of the current handle.
msg234865 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-01-27 23:34
New changeset d61d1e73674f by Victor Stinner in branch '3.4':
asyncio: sync with Tulip
https://hg.python.org/cpython/rev/d61d1e73674f
History
Date User Action Args
2015-01-27 23:34:58python-devsetmessages: + msg234865
2015-01-26 14:06:06vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg234745
2015-01-26 10:07:33python-devsetnosy: + python-dev
messages: + msg234728
2015-01-20 22:42:05vstinnersettitle: asyncio: add BaseEventLoop._current_handle -> asyncio: add BaseEventLoop._current_handle (only used in debug mode)
2015-01-20 21:51:34yselivanovsetmessages: + msg234396
2015-01-20 21:40:39vstinnersetmessages: + msg234395
2015-01-09 15:41:00vstinnersetmessages: + msg233760
2015-01-09 15:34:55vstinnercreate