def test_kernel(self): with self.assertRaises(LookupError): contexts.get_kernel() token = contexts.set_kernel(42) self.assertEqual(contexts.get_kernel(), 42) contexts.KERNEL.reset(token) with self.assertRaises(LookupError): contexts.get_kernel()
def unblock(self): """Unblock current waiters.""" # Let's make a special case for calling ``unblock`` out of a # kernel context. try: contexts.get_kernel().unblock(self) except LookupError: pass
def set(self): self._flag = True # Let's make a special case when no task is waiting for this # event object (with this, you may call ``Event.set`` out of a # kernel context). This is useful when you want to initialize # events before a kernel context is initialized. try: contexts.get_kernel().unblock(self) except LookupError: pass
def test_event(self): with self.assertRaises(LookupError): contexts.get_kernel() e = locks.Event() self.assertFalse(e.is_set()) e.set() self.assertTrue(e.is_set()) e.set() # You may call it repeatedly. self.assertTrue(e.is_set()) e.clear() self.assertFalse(e.is_set()) e.clear() # You may call it repeatedly. self.assertFalse(e.is_set())
def __init__(self, duration, *, task=None): kernel = contexts.get_kernel() if not task: task = kernel.get_current_task() if not task: raise LookupError('no current task: %r' % kernel) self._cancel = kernel.timeout_after(task, duration)
def __init__(self, file): super().__init__(file, self.PROXIED_FIELDS) self.__file = file os.set_blocking(self.__file.fileno(), False) kernel = contexts.get_kernel() kernel.notify_open(self.__file.fileno()) # Keep a weak reference to kernel because we could call # `notify_close` in another thread. self.__kernel = weakref.ref(kernel)
def __init__(self, sock): super().__init__(sock, self.PROXIED_FIELDS) self.__sock = sock self.__sock.setblocking(False) kernel = contexts.get_kernel() kernel.notify_open(self.__sock.fileno()) # Keep a weak reference to kernel because we could call # `notify_close` in another thread. self.__kernel = weakref.ref(kernel)
def test_queue(self): with self.assertRaises(LookupError): contexts.get_kernel() tq = tasks.CompletionQueue() self.assertFalse(tq.is_closed()) self.assertFalse(tq) self.assertEqual(len(tq), 0) task = Task(None, square(7)) tq.put_nonblocking(task) self.assertFalse(tq.is_closed()) self.assertTrue(tq) self.assertEqual(len(tq), 1) tq.close() self.assertTrue(tq.is_closed()) self.assertTrue(tq) self.assertEqual(len(tq), 1) self.assertIsNone(task.tick(None, None))
async def join(self): if self.__future.is_completed(): return # Since the callback could be fired from another thread, which # may not have the right kernel object in its context, we should # get the right kernel object from the context here, and pass it # to the callback function. kernel = contexts.get_kernel() callback = lambda: kernel.unblock(self.__future) await traps.block( self.__future, lambda: self.__future. add_callback(lambda _: kernel.post_callback(callback)), ) ASSERT.true(self.__future.is_completed())
def get_current_task(): # You may call this out of a kernel context. kernel = contexts.get_kernel(None) return kernel.get_current_task() if kernel else None
def get_all_tasks(): # You may call this out of a kernel context. kernel = contexts.get_kernel(None) return kernel.get_all_tasks() if kernel else []
def spawn(awaitable): return contexts.get_kernel().spawn(awaitable)
def release(self): ASSERT.true(self.is_owner()) self._locked = False contexts.get_kernel().unblock(self)
def test_gate(self): with self.assertRaises(LookupError): contexts.get_kernel() g = locks.Gate() g.unblock()