Reviewers: gvrpython,

Message:
The assertion introduced by this change has already been helpful in
identifying incorrect thread usage in a prototype @thread_affinity
decorator I have been writing.


https://codereview.appspot.com/67400043/diff/1/asyncio/base_events.py
File asyncio/base_events.py (right):

https://codereview.appspot.com/67400043/diff/1/asyncio/base_events.py#newcode281
asyncio/base_events.py:281: "call_soon() invoked on event loop other
than current one")
I would prefer to use a more specific exception such as an equivalent to
Java's IllegalStateException, but I am not aware of any such exception
for Python.

Description:
Ensure call_soon() invoked on current loop when get_debug() enabled

Please review this at https://codereview.appspot.com/67400043/

Affected files (+6, -2 lines):
  M asyncio/base_events.py


Index: asyncio/base_events.py
===================================================================
--- a/asyncio/base_events.py
+++ b/asyncio/base_events.py
@@ -263,7 +263,7 @@
         heapq.heappush(self._scheduled, timer)
         return timer

-    def call_soon(self, callback, *args):
+    def call_soon(self, callback, *args, _check_thread=True):
         """Arrange for a callback to be called as soon as possible.

         This operates as a FIFO queue, callbacks are called in the
@@ -275,13 +275,17 @@
         """
         if tasks.iscoroutinefunction(callback):
             raise TypeError("coroutines cannot be used with call_soon()")
+        if _check_thread and self.get_debug() and \
+            events.get_event_loop() != self:
+            raise AssertionError(
+                "call_soon() invoked on event loop other than current one")
         handle = events.Handle(callback, args, self)
         self._ready.append(handle)
         return handle

     def call_soon_threadsafe(self, callback, *args):
         """XXX"""
-        handle = self.call_soon(callback, *args)
+        handle = self.call_soon(callback, *args, _check_thread=False)
         self._write_to_self()
         return handle



Reply via email to