def test_wrap_future_use_global_loop(self, m_events): def run(arg): return (arg, threading.get_ident()) ex = concurrent.futures.ThreadPoolExecutor(1) f1 = ex.submit(run, 'oi') f2 = futures.wrap_future(f1) self.assertIs(m_events.get_event_loop.return_value, f2._loop)
def test_wrap_future_cancel(self): f1 = concurrent.futures.Future() f2 = futures.wrap_future(f1, loop=self.loop) f2.cancel() test_utils.run_briefly(self.loop) self.assertTrue(f1.cancelled()) self.assertTrue(f2.cancelled())
def test_wrap_future_cancel2(self): f1 = concurrent.futures.Future() f2 = futures.wrap_future(f1, loop=self.loop) f1.set_result(42) f2.cancel() test_utils.run_briefly(self.loop) self.assertFalse(f1.cancelled()) self.assertEqual(f1.result(), 42) self.assertTrue(f2.cancelled())
def test_wrap_future(self): def run(arg): time.sleep(0.1) return arg ex = concurrent.futures.ThreadPoolExecutor(1) f1 = ex.submit(run, 'oi') f2 = futures.wrap_future(f1, loop=self.loop) res = self.loop.run_until_complete(f2) self.assertIsInstance(f2, futures.Future) self.assertEqual(res, 'oi')
def test_wrap_future(self): def run(arg): return (arg, threading.get_ident()) ex = concurrent.futures.ThreadPoolExecutor(1) f1 = ex.submit(run, 'oi') f2 = futures.wrap_future(f1, loop=self.loop) res, ident = self.loop.run_until_complete(f2) self.assertIsInstance(f2, futures.Future) self.assertEqual(res, 'oi') self.assertNotEqual(ident, threading.get_ident())
def run_in_executor(self, executor, callback, *args): if isinstance(callback, events.Handle): assert not args assert not isinstance(callback, events.TimerHandle) if callback._cancelled: f = futures.Future() f.set_result(None) return f callback, args = callback._callback, callback._args if executor is None: executor = self._default_executor if executor is None: executor = concurrent.futures.ThreadPoolExecutor(_MAX_WORKERS) self._default_executor = executor return futures.wrap_future(executor.submit(callback, *args), loop=self)
def run_in_executor(self, executor, callback, *args): if coroutines.iscoroutinefunction(callback): raise TypeError("Coroutines cannot be used with run_in_executor()") if isinstance(callback, events.Handle): assert not args assert not isinstance(callback, events.TimerHandle) if callback._cancelled: f = futures.Future(loop=self) f.set_result(None) return f callback, args = callback._callback, callback._args if executor is None: executor = self._default_executor if executor is None: executor = concurrent.futures.ThreadPoolExecutor(_MAX_WORKERS) self._default_executor = executor return futures.wrap_future(executor.submit(callback, *args), loop=self)
def run_in_executor(self, executor, callback, *args): if tasks.iscoroutinefunction(callback): raise TypeError("coroutines cannot be used with run_in_executor()") if isinstance(callback, events.Handle): assert not args assert not isinstance(callback, events.TimerHandle) if callback._cancelled: f = futures.Future(loop=self) f.set_result(None) return f callback, args = callback._callback, callback._args if executor is None: executor = self._default_executor if executor is None: executor = concurrent.futures.ThreadPoolExecutor(_MAX_WORKERS) self._default_executor = executor return futures.wrap_future(executor.submit(callback, *args), loop=self)
def test_wrap_future_future(self): f1 = futures.Future(loop=self.loop) f2 = futures.wrap_future(f1) self.assertIs(f1, f2)