Skip to content

Commit fed3c91

Browse files
authored
Merge pull request #8 from gistart/pr/cancel-fix
minor cancel() improvements
2 parents 7814e7f + 5265260 commit fed3c91

File tree

5 files changed

+22
-21
lines changed

5 files changed

+22
-21
lines changed

asyncio_pool/base_pool.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ async def _spawn(self, future, coro, cb=None, ctx=None):
143143
await self.semaphore.acquire()
144144
except BaseException as e:
145145
acq_error = True
146+
coro.close()
146147
if not future.done():
147148
future.set_exception(e)
148149
finally:
@@ -273,8 +274,10 @@ async def cancel(self, *futures, get_result=getres.flat):
273274
tasks.append(task)
274275
_futures.append(fut)
275276

276-
cancelled = sum(1 for task in tasks if task.cancel())
277-
await aio.wait(tasks) # let them actually cancel
277+
cancelled = 0
278+
if tasks:
279+
cancelled = sum(1 for task in tasks if task.cancel())
280+
await aio.wait(tasks) # let them actually cancel
278281
# need to collect them anyway, to supress warnings
279282
return cancelled, [get_result(fut) for fut in _futures]
280283

tests/test_base.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
# coding: utf8
22

3-
import sys
43
import pytest
5-
import warnings
64
import asyncio as aio
7-
from itertools import chain
85
from asyncio_pool import AioPool
96
from async_timeout import timeout
107

118

12-
pytestmark = pytest.mark.filterwarnings('ignore:coroutine')
13-
14-
159
@pytest.mark.asyncio
1610
async def test_concurrency():
1711
todo = range(1,21)
@@ -91,7 +85,6 @@ async def wrk_safe(*arg, **kw):
9185
await aio.sleep(0.5)
9286
except aio.CancelledError:
9387
await aio.sleep(0.1) # simulate cleanup
94-
pass
9588
return 1
9689

9790
pool = AioPool(size=5)

tests/test_callbacks.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
# coding: utf8
22

3-
import sys
43
import pytest
54
import asyncio as aio
65
from asyncio_pool import AioPool, getres
76

87

9-
pytestmark = pytest.mark.filterwarnings('ignore:coroutine')
10-
11-
128
async def cb(res, err, ctx):
139
if err:
1410
exc, tb = err

tests/test_map.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
# coding: utf8
22

3-
import sys
43
import pytest
54
import asyncio as aio
65
from asyncio_pool import AioPool, getres
76

87

9-
pytestmark = pytest.mark.filterwarnings('ignore:coroutine')
10-
11-
128
async def wrk(n):
139
await aio.sleep(1 / n)
1410
return n*10
@@ -56,3 +52,20 @@ async def wrk(n):
5652
else:
5753
assert False # should not get here
5854
i += 1 # does not support enumerate btw (
55+
56+
57+
@pytest.mark.asyncio
58+
async def test_itermap_cancel():
59+
60+
async def wrk(n):
61+
await aio.sleep(n / 100)
62+
return n
63+
64+
todo = range(1, 101)
65+
66+
async with AioPool(5) as pool:
67+
async for res in pool.itermap(wrk, todo, yield_when=aio.FIRST_COMPLETED):
68+
if res == 13:
69+
cancelled, _ = await pool.cancel()
70+
break
71+
assert cancelled == 100 - 13

tests/test_spawn.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
# coding: utf8
22

3-
import sys
43
import pytest
54
import asyncio as aio
65
from asyncio_pool import AioPool
76

87

9-
pytestmark = pytest.mark.filterwarnings('ignore:coroutine')
10-
11-
128
@pytest.mark.asyncio
139
async def test_spawns_behaviour():
1410
started = []

0 commit comments

Comments
 (0)