|
1 | 1 | """ |
2 | 2 | Helper functions used by the Drycc server. |
3 | 3 | """ |
4 | | -import asyncio |
5 | 4 | import base64 |
6 | 5 | import concurrent |
7 | 6 | import hashlib |
@@ -145,43 +144,22 @@ def dict_merge(origin, merge): |
145 | 144 | return result |
146 | 145 |
|
147 | 146 |
|
148 | | -def async_run(tasks): |
| 147 | +def apply_tasks(tasks): |
149 | 148 | """ |
150 | 149 | run a group of tasks async |
151 | 150 | Requires the tasks arg to be a list of functools.partial() |
152 | 151 | """ |
153 | 152 | if not tasks: |
154 | 153 | return |
155 | 154 |
|
156 | | - # start a new async event loop |
157 | | - loop = asyncio.get_event_loop() |
158 | | - # https://github.com/python/asyncio/issues/258 |
159 | 155 | executor = concurrent.futures.ThreadPoolExecutor(5) |
160 | | - loop.set_default_executor(executor) |
161 | | - |
162 | | - async_tasks = [asyncio.ensure_future(async_task(task, loop)) for task in tasks] |
163 | | - # run tasks in parallel |
164 | | - loop.run_until_complete(asyncio.wait(async_tasks)) |
165 | | - # deal with errors (exceptions, etc) |
166 | | - for task in async_tasks: |
167 | | - error = task.exception() |
| 156 | + for future in [executor.submit(task) for task in tasks]: |
| 157 | + error = future.exception() |
168 | 158 | if error is not None: |
169 | 159 | raise error |
170 | | - |
171 | 160 | executor.shutdown(wait=True) |
172 | 161 |
|
173 | 162 |
|
174 | | -async def async_task(params, loop): |
175 | | - """ |
176 | | - Perform a task asynchronously. |
177 | | - """ |
178 | | - # get the calling function |
179 | | - logger.debug('Running {}'.format(params)) |
180 | | - # This executes a task in its own thread (in parallel) |
181 | | - await loop.run_in_executor(None, params) |
182 | | - logger.debug('Finished running {}'.format(params)) |
183 | | - |
184 | | - |
185 | 163 | if __name__ == "__main__": |
186 | 164 | import doctest |
187 | 165 | doctest.testmod() |
0 commit comments