2015-01-19 15:55 GMT+01:00 Luciano Ramalho <
luc...@ramalho.org>:
> I am sorry, but I can't reconcile that with this quote from Guido's
> last comment closing your bug report [1]. What am I missing?
Event loops are more "global" than other resources like files,
subprocesses, locks, IMAP or FTP connections (classes implementing
__exit__). asyncio.get_event_loop() doesn't tell you if the event loop
already existed or not.
I disagree with Guido on this issue (I would like to support the
context manager protocol), so I cannot explain you his point of view
:-)
> In a GUI application you may know it's about to exit but you don't own
> the event loop.
Who created the loop and who run the blocking run_forever() function?
If it's a library, the library should be responsible to call
loop.close(), or provide a function which calls loop.close().
All asyncio examples have this pattern in the "main" function:
loop = asyncio.get_event_loop()
# ... use the event loop ...
loop.close()
> If AbstractEventLoopPolicy.get_event_loop() exists to accommodate
> environments providing event loops in their different ways, then the
> AbstractEventLoopPolicy interface should have a function that could be
> safely called by anyone to notify the environment you don't need the
> loop anymore. Such a function -- maybe release_event_loop() -- could
> be called by a BaseEventLoop.release() instance method and by a
> BaseEventLoop.__exit__ method.
I don't understand your proposition. It looks like a reference
counter. Almost all functions in asyncio may call get_event_loop().
Having to call a "release()" method would make the code much more
verbose. It's tricky to ensure that the method is called even if an
exception was raised, and decide when we are done with the event loop.
Victor