Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1cf296

Browse files
committed
asyncio: Remove asyncio.timeout() context manager.
It will probably be added back in Python 3.6, once its compatibility issues are resolved; see [1] for more details. [1] https://mail.python.org/pipermail/async-sig/2016-June/000045.html
1 parent af74512 commit c1cf296

4 files changed

Lines changed: 0 additions & 260 deletions

File tree

Doc/library/asyncio-task.rst

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -662,22 +662,6 @@ Task functions
662662
except CancelledError:
663663
res = None
664664

665-
.. function:: timeout(timeout, \*, loop=None)
666-
667-
Return a context manager that cancels a block on *timeout* expiring::
668-
669-
with timeout(1.5):
670-
yield from inner()
671-
672-
1. If ``inner()`` is executed faster than in ``1.5`` seconds
673-
nothing happens.
674-
2. Otherwise ``inner()`` is cancelled internally but
675-
:exc:`asyncio.TimeoutError` is raised outside of
676-
context manager scope.
677-
678-
Passing ``None`` as *timeout* argument disables the manager logic.
679-
680-
.. versionadded:: 3.5.2
681665

682666
.. coroutinefunction:: wait(futures, \*, loop=None, timeout=None,\
683667
return_when=ALL_COMPLETED)

Doc/whatsnew/3.5.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -835,10 +835,6 @@ Updates in 3.5.2:
835835
method to get the current exception handler.
836836
(Contributed by Yury Selivanov.)
837837

838-
* New :func:`~asyncio.timeout` context manager to simplify timeouts
839-
handling code.
840-
(Contributed by Andrew Svetlov.)
841-
842838
* New :meth:`StreamReader.readuntil() <asyncio.StreamReader.readuntil>`
843839
method to read data from the stream until a separator bytes
844840
sequence appears.

Lib/asyncio/tasks.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
'FIRST_COMPLETED', 'FIRST_EXCEPTION', 'ALL_COMPLETED',
55
'wait', 'wait_for', 'as_completed', 'sleep', 'async',
66
'gather', 'shield', 'ensure_future', 'run_coroutine_threadsafe',
7-
'timeout',
87
]
98

109
import concurrent.futures
@@ -737,55 +736,3 @@ def callback():
737736

738737
loop.call_soon_threadsafe(callback)
739738
return future
740-
741-
742-
def timeout(timeout, *, loop=None):
743-
"""A factory which produce a context manager with timeout.
744-
745-
Useful in cases when you want to apply timeout logic around block
746-
of code or in cases when asyncio.wait_for is not suitable.
747-
748-
For example:
749-
750-
>>> with asyncio.timeout(0.001):
751-
... yield from coro()
752-
753-
754-
timeout: timeout value in seconds or None to disable timeout logic
755-
loop: asyncio compatible event loop
756-
"""
757-
if loop is None:
758-
loop = events.get_event_loop()
759-
return _Timeout(timeout, loop=loop)
760-
761-
762-
class _Timeout:
763-
def __init__(self, timeout, *, loop):
764-
self._timeout = timeout
765-
self._loop = loop
766-
self._task = None
767-
self._cancelled = False
768-
self._cancel_handler = None
769-
770-
def __enter__(self):
771-
self._task = Task.current_task(loop=self._loop)
772-
if self._task is None:
773-
raise RuntimeError('Timeout context manager should be used '
774-
'inside a task')
775-
if self._timeout is not None:
776-
self._cancel_handler = self._loop.call_later(
777-
self._timeout, self._cancel_task)
778-
return self
779-
780-
def __exit__(self, exc_type, exc_val, exc_tb):
781-
if exc_type is futures.CancelledError and self._cancelled:
782-
self._cancel_handler = None
783-
self._task = None
784-
raise futures.TimeoutError
785-
if self._timeout is not None:
786-
self._cancel_handler.cancel()
787-
self._cancel_handler = None
788-
self._task = None
789-
790-
def _cancel_task(self):
791-
self._cancelled = self._task.cancel()

Lib/test/test_asyncio/test_tasks.py

Lines changed: 0 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,192 +2260,5 @@ def coro():
22602260
self.assertEqual(result, 11)
22612261

22622262

2263-
class TimeoutTests(test_utils.TestCase):
2264-
def setUp(self):
2265-
self.loop = asyncio.new_event_loop()
2266-
asyncio.set_event_loop(None)
2267-
2268-
def tearDown(self):
2269-
self.loop.close()
2270-
self.loop = None
2271-
2272-
def test_timeout(self):
2273-
canceled_raised = [False]
2274-
2275-
@asyncio.coroutine
2276-
def long_running_task():
2277-
try:
2278-
yield from asyncio.sleep(10, loop=self.loop)
2279-
except asyncio.CancelledError:
2280-
canceled_raised[0] = True
2281-
raise
2282-
2283-
@asyncio.coroutine
2284-
def go():
2285-
with self.assertRaises(asyncio.TimeoutError):
2286-
with asyncio.timeout(0.01, loop=self.loop) as t:
2287-
yield from long_running_task()
2288-
self.assertIs(t._loop, self.loop)
2289-
2290-
self.loop.run_until_complete(go())
2291-
self.assertTrue(canceled_raised[0], 'CancelledError was not raised')
2292-
2293-
def test_timeout_finish_in_time(self):
2294-
@asyncio.coroutine
2295-
def long_running_task():
2296-
yield from asyncio.sleep(0.01, loop=self.loop)
2297-
return 'done'
2298-
2299-
@asyncio.coroutine
2300-
def go():
2301-
with asyncio.timeout(0.1, loop=self.loop):
2302-
resp = yield from long_running_task()
2303-
self.assertEqual(resp, 'done')
2304-
2305-
self.loop.run_until_complete(go())
2306-
2307-
def test_timeout_gloabal_loop(self):
2308-
asyncio.set_event_loop(self.loop)
2309-
2310-
@asyncio.coroutine
2311-
def run():
2312-
with asyncio.timeout(0.1) as t:
2313-
yield from asyncio.sleep(0.01)
2314-
self.assertIs(t._loop, self.loop)
2315-
2316-
self.loop.run_until_complete(run())
2317-
2318-
def test_timeout_not_relevant_exception(self):
2319-
@asyncio.coroutine
2320-
def go():
2321-
yield from asyncio.sleep(0, loop=self.loop)
2322-
with self.assertRaises(KeyError):
2323-
with asyncio.timeout(0.1, loop=self.loop):
2324-
raise KeyError
2325-
2326-
self.loop.run_until_complete(go())
2327-
2328-
def test_timeout_canceled_error_is_converted_to_timeout(self):
2329-
@asyncio.coroutine
2330-
def go():
2331-
yield from asyncio.sleep(0, loop=self.loop)
2332-
with self.assertRaises(asyncio.CancelledError):
2333-
with asyncio.timeout(0.001, loop=self.loop):
2334-
raise asyncio.CancelledError
2335-
2336-
self.loop.run_until_complete(go())
2337-
2338-
def test_timeout_blocking_loop(self):
2339-
@asyncio.coroutine
2340-
def long_running_task():
2341-
time.sleep(0.05)
2342-
return 'done'
2343-
2344-
@asyncio.coroutine
2345-
def go():
2346-
with asyncio.timeout(0.01, loop=self.loop):
2347-
result = yield from long_running_task()
2348-
self.assertEqual(result, 'done')
2349-
2350-
self.loop.run_until_complete(go())
2351-
2352-
def test_for_race_conditions(self):
2353-
fut = asyncio.Future(loop=self.loop)
2354-
self.loop.call_later(0.1, fut.set_result('done'))
2355-
2356-
@asyncio.coroutine
2357-
def go():
2358-
with asyncio.timeout(0.2, loop=self.loop):
2359-
resp = yield from fut
2360-
self.assertEqual(resp, 'done')
2361-
2362-
self.loop.run_until_complete(go())
2363-
2364-
def test_timeout_time(self):
2365-
@asyncio.coroutine
2366-
def go():
2367-
foo_running = None
2368-
2369-
start = self.loop.time()
2370-
with self.assertRaises(asyncio.TimeoutError):
2371-
with asyncio.timeout(0.1, loop=self.loop):
2372-
foo_running = True
2373-
try:
2374-
yield from asyncio.sleep(0.2, loop=self.loop)
2375-
finally:
2376-
foo_running = False
2377-
2378-
dt = self.loop.time() - start
2379-
# tolerate a small delta for slow delta or unstable clocks
2380-
self.assertTrue(0.09 < dt < 0.12, dt)
2381-
self.assertFalse(foo_running)
2382-
2383-
self.loop.run_until_complete(go())
2384-
2385-
def test_timeout_disable(self):
2386-
@asyncio.coroutine
2387-
def long_running_task():
2388-
yield from asyncio.sleep(0.1, loop=self.loop)
2389-
return 'done'
2390-
2391-
@asyncio.coroutine
2392-
def go():
2393-
t0 = self.loop.time()
2394-
with asyncio.timeout(None, loop=self.loop):
2395-
resp = yield from long_running_task()
2396-
self.assertEqual(resp, 'done')
2397-
dt = self.loop.time() - t0
2398-
# tolerate a time delta for clocks with bad resolution
2399-
# and slow buildbots
2400-
self.assertTrue(0.09 < dt < 0.15, dt)
2401-
self.loop.run_until_complete(go())
2402-
2403-
def test_raise_runtimeerror_if_no_task(self):
2404-
with self.assertRaises(RuntimeError):
2405-
with asyncio.timeout(0.1, loop=self.loop):
2406-
pass
2407-
2408-
def test_outer_coro_is_not_cancelled(self):
2409-
2410-
has_timeout = [False]
2411-
2412-
@asyncio.coroutine
2413-
def outer():
2414-
try:
2415-
with asyncio.timeout(0.001, loop=self.loop):
2416-
yield from asyncio.sleep(1, loop=self.loop)
2417-
except asyncio.TimeoutError:
2418-
has_timeout[0] = True
2419-
2420-
@asyncio.coroutine
2421-
def go():
2422-
task = asyncio.ensure_future(outer(), loop=self.loop)
2423-
yield from task
2424-
self.assertTrue(has_timeout[0])
2425-
self.assertFalse(task.cancelled())
2426-
self.assertTrue(task.done())
2427-
2428-
self.loop.run_until_complete(go())
2429-
2430-
def test_cancel_outer_coro(self):
2431-
fut = asyncio.Future(loop=self.loop)
2432-
2433-
@asyncio.coroutine
2434-
def outer():
2435-
fut.set_result(None)
2436-
yield from asyncio.sleep(1, loop=self.loop)
2437-
2438-
@asyncio.coroutine
2439-
def go():
2440-
task = asyncio.ensure_future(outer(), loop=self.loop)
2441-
yield from fut
2442-
task.cancel()
2443-
with self.assertRaises(asyncio.CancelledError):
2444-
yield from task
2445-
self.assertTrue(task.cancelled())
2446-
self.assertTrue(task.done())
2447-
2448-
self.loop.run_until_complete(go())
2449-
24502263
if __name__ == '__main__':
24512264
unittest.main()

0 commit comments

Comments
 (0)