Skip to content

Instantly share code, notes, and snippets.

@muayyad-alsadi
Last active March 29, 2021 17:05
Show Gist options
  • Save muayyad-alsadi/287d668686298a43f1ae9585c0f98e5d to your computer and use it in GitHub Desktop.
Save muayyad-alsadi/287d668686298a43f1ae9585c0f98e5d to your computer and use it in GitHub Desktop.
aiotools unordered_bulks
import asyncio
# also consider asyncio.Semaphore(10) see https://docs.python.org/3/library/asyncio-sync.html#semaphore
async def unordered_bulks(size, aiterable):
tasks = set()
async for task in aiterable:
tasks.add(task)
if len(tasks)>=size:
for coro in asyncio.as_completed(tasks):
yield coro
tasks.clear()
if len(tasks):
for coro in asyncio.as_completed(tasks):
yield coro
tasks.clear()
async def rndsleep(i):
print("entering ", i)
rnd=random.randrange(100, 200)/1000.0
await asyncio.sleep(rnd)
print("leaving ", i)
return "** {}: {} ms".format(i, rnd)
async def myloop(n):
await asyncio.sleep(0.1)
for i in range(n):
task = asyncio.create_task(rndsleep(i))
yield task
async def main():
tasks = myloop(100)
async for task in unordered_bulks(5, tasks):
print(await task)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
entering 0
entering 1
entering 2
entering 3
entering 4
leaving 4
** 4: 0.106 ms
leaving 0
** 0: 0.116 ms
leaving 1
** 1: 0.174 ms
leaving 3
** 3: 0.185 ms
leaving 2
** 2: 0.189 ms
entering 5
entering 6
entering 7
entering 8
entering 9
leaving 5
** 5: 0.154 ms
leaving 7
** 7: 0.165 ms
leaving 9
** 9: 0.185 ms
leaving 6
** 6: 0.194 ms
leaving 8
** 8: 0.196 ms
entering 10
entering 11
entering 12
entering 13
entering 14
leaving 10
** 10: 0.113 ms
leaving 13
** 13: 0.115 ms
leaving 11
** 11: 0.129 ms
leaving 12
** 12: 0.143 ms
leaving 14
** 14: 0.186 ms
entering 15
entering 16
entering 17
entering 18
entering 19
leaving 19
** 19: 0.1 ms
leaving 18
** 18: 0.119 ms
leaving 16
** 16: 0.152 ms
leaving 15
** 15: 0.159 ms
leaving 17
** 17: 0.172 ms
entering 20
entering 21
entering 22
entering 23
entering 24
leaving 21
** 21: 0.102 ms
leaving 23
** 23: 0.135 ms
leaving 24
** 24: 0.139 ms
leaving 20
** 20: 0.178 ms
leaving 22
** 22: 0.196 ms
entering 25
entering 26
entering 27
entering 28
entering 29
leaving 27
** 27: 0.101 ms
leaving 29
** 29: 0.106 ms
leaving 25
** 25: 0.13 ms
leaving 26
** 26: 0.138 ms
leaving 28
** 28: 0.192 ms
entering 30
entering 31
entering 32
entering 33
entering 34
leaving 30
** 30: 0.13 ms
leaving 33
** 33: 0.135 ms
leaving 31
** 31: 0.144 ms
leaving 34
** 34: 0.147 ms
leaving 32
** 32: 0.187 ms
entering 35
entering 36
entering 37
entering 38
entering 39
leaving 39
** 39: 0.127 ms
leaving 37
** 37: 0.13 ms
leaving 36
** 36: 0.172 ms
leaving 35
** 35: 0.186 ms
leaving 38
** 38: 0.195 ms
entering 40
entering 41
entering 42
entering 43
entering 44
leaving 40
** 40: 0.127 ms
leaving 43
** 43: 0.148 ms
leaving 44
** 44: 0.156 ms
leaving 41
** 41: 0.173 ms
leaving 42
** 42: 0.199 ms
entering 45
entering 46
entering 47
entering 48
entering 49
leaving 45
** 45: 0.108 ms
leaving 47
** 47: 0.109 ms
leaving 49
** 49: 0.115 ms
leaving 46
** 46: 0.185 ms
leaving 48
** 48: 0.198 ms
entering 50
entering 51
entering 52
entering 53
entering 54
leaving 51
** 51: 0.107 ms
leaving 50
** 50: 0.131 ms
leaving 52
** 52: 0.142 ms
leaving 54
** 54: 0.143 ms
leaving 53
** 53: 0.159 ms
entering 55
entering 56
entering 57
entering 58
entering 59
leaving 55
** 55: 0.118 ms
leaving 59
** 59: 0.128 ms
leaving 58
** 58: 0.158 ms
leaving 56
** 56: 0.187 ms
leaving 57
** 57: 0.198 ms
entering 60
entering 61
entering 62
entering 63
entering 64
leaving 62
** 62: 0.107 ms
leaving 64
** 64: 0.111 ms
leaving 61
** 61: 0.177 ms
leaving 63
** 63: 0.179 ms
leaving 60
** 60: 0.18 ms
entering 65
entering 66
entering 67
entering 68
entering 69
leaving 67
** 67: 0.164 ms
leaving 68
leaving 66
** 68: 0.171 ms
** 66: 0.172 ms
leaving 69
** 69: 0.174 ms
leaving 65
** 65: 0.193 ms
entering 70
entering 71
entering 72
entering 73
entering 74
leaving 71
** 71: 0.139 ms
leaving 72
** 72: 0.157 ms
leaving 70
** 70: 0.178 ms
leaving 74
leaving 73
** 74: 0.183 ms
** 73: 0.184 ms
entering 75
entering 76
entering 77
entering 78
entering 79
leaving 79
** 79: 0.113 ms
leaving 77
** 77: 0.121 ms
leaving 78
** 78: 0.168 ms
leaving 76
** 76: 0.17 ms
leaving 75
** 75: 0.186 ms
entering 80
entering 81
entering 82
entering 83
entering 84
leaving 82
leaving 83
** 82: 0.113 ms
** 83: 0.113 ms
leaving 84
** 84: 0.119 ms
leaving 80
** 80: 0.142 ms
leaving 81
** 81: 0.191 ms
entering 85
entering 86
entering 87
entering 88
entering 89
leaving 86
** 86: 0.101 ms
leaving 87
leaving 88
** 87: 0.146 ms
** 88: 0.146 ms
leaving 89
** 89: 0.165 ms
leaving 85
** 85: 0.178 ms
entering 90
entering 91
entering 92
entering 93
entering 94
leaving 91
** 91: 0.135 ms
leaving 94
** 94: 0.138 ms
leaving 90
** 90: 0.173 ms
leaving 93
** 93: 0.181 ms
leaving 92
** 92: 0.196 ms
entering 95
entering 96
entering 97
entering 98
entering 99
leaving 95
** 95: 0.127 ms
leaving 99
** 99: 0.141 ms
leaving 96
leaving 97
** 96: 0.193 ms
** 97: 0.193 ms
leaving 98
** 98: 0.196 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment