Skip to content

Commit b04a724

Browse files
committed
Make spawn_n and map_n synchronous
These functions are specifically not blocking or awaiting, so they don't need to be coroutines either. Breaking change!
1 parent 34577f0 commit b04a724

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

asyncio_pool/base_pool.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ async def spawn(self, coro, cb=None, ctx=None):
182182
self._waiting[future] = self.loop.create_future() # as a placeholder
183183
return await self._spawn(future, coro, cb=cb, ctx=ctx)
184184

185-
async def spawn_n(self, coro, cb=None, ctx=None):
185+
def spawn_n(self, coro, cb=None, ctx=None):
186186
'''Creates waiting task for given `coro` regardless of pool space. If
187187
pool is not full, this task will be executed very soon. Main difference
188188
is that `spawn_n` does not block and returns future very quickly.
@@ -203,15 +203,15 @@ async def exec(self, coro, cb=None, ctx=None):
203203
'''
204204
return await (await self.spawn(coro, cb, ctx))
205205

206-
async def map_n(self, fn, iterable, cb=None, ctx=None):
206+
def map_n(self, fn, iterable, cb=None, ctx=None):
207207
'''Creates coroutine with `fn` function for each item in `iterable`,
208208
spawns each of them with `spawn_n`, returning futures.
209209
210210
Read more about callbacks in `spawn` docstring.
211211
'''
212212
futures = []
213213
for it in iterable:
214-
fut = await self.spawn_n(fn(it), cb, ctx)
214+
fut = self.spawn_n(fn(it), cb, ctx)
215215
futures.append(fut)
216216
return futures
217217

tests/test_base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async def wrk(n):
2626

2727
pool_size = 5
2828
async with AioPool(size=pool_size) as pool:
29-
futures = await pool.map_n(wrk, todo)
29+
futures = pool.map_n(wrk, todo)
3030

3131
await aio.sleep(0.01)
3232

@@ -70,7 +70,7 @@ async def outer(n, pool):
7070
loop = aio.get_event_loop()
7171
pool = AioPool(size=100)
7272

73-
tasks = await pool.map_n(inner, todo)
73+
tasks = pool.map_n(inner, todo)
7474
joined = [loop.create_task(outer(j, pool)) for j in to_release]
7575
await pool.join()
7676

@@ -88,9 +88,9 @@ async def wrk(*arg, **kw):
8888

8989
pool = AioPool(size=2)
9090

91-
f_quick = await pool.spawn_n(aio.sleep(0.15))
92-
f12 = await pool.spawn(wrk()), await pool.spawn_n(wrk())
93-
f35 = await pool.map_n(wrk, range(3))
91+
f_quick = pool.spawn_n(aio.sleep(0.15))
92+
f12 = await pool.spawn(wrk()), pool.spawn_n(wrk())
93+
f35 = pool.map_n(wrk, range(3))
9494

9595
# cancel some
9696
await aio.sleep(0.1)

0 commit comments

Comments
 (0)