|
4 | 4 | import json |
5 | 5 | from functools import partial |
6 | 6 |
|
| 7 | +import concurrent.futures |
| 8 | +import multiprocessing as mp |
7 | 9 | import googleapiclient.errors |
8 | 10 | import numpy as np |
| 11 | +from tqdm import tqdm |
9 | 12 |
|
10 | 13 | from cloudvolume.threaded_queue import ThreadedQueue |
11 | 14 |
|
|
16 | 19 | from .secrets import PROJECT_NAME, QUEUE_NAME, QUEUE_TYPE |
17 | 20 |
|
18 | 21 | def totask(task): |
19 | | - print(task) |
20 | 22 | taskobj = payloadBase64Decode(task['payloadBase64']) |
21 | 23 | taskobj._id = task['id'] |
22 | 24 | return taskobj |
@@ -231,3 +233,36 @@ def __enter__(self): |
231 | 233 |
|
232 | 234 | def __exit__(self, exception_type, exception_value, traceback): |
233 | 235 | pass |
| 236 | + |
| 237 | +class LocalTaskQueue(object): |
| 238 | + def __init__(self, parallel=1, queue_name='', queue_server='', progress=True): |
| 239 | + if parallel and type(parallel) == bool: |
| 240 | + parallel = mp.cpu_count() |
| 241 | + |
| 242 | + self.parallel = parallel |
| 243 | + self.queue = [] |
| 244 | + self.progress = progress |
| 245 | + |
| 246 | + def insert(self, task): |
| 247 | + self.queue.append(task) |
| 248 | + |
| 249 | + def wait(self, progress=None): |
| 250 | + return self |
| 251 | + |
| 252 | + def kill_threads(self): |
| 253 | + return self |
| 254 | + |
| 255 | + def __enter__(self): |
| 256 | + return self |
| 257 | + |
| 258 | + def __exit__(self, exception_type, exception_value, traceback): |
| 259 | + with tqdm(total=len(self.queue), desc="Tasks") as pbar: |
| 260 | + with concurrent.futures.ProcessPoolExecutor(max_workers=self.parallel) as executor: |
| 261 | + for _ in executor.map(task_execute, self.queue): |
| 262 | + pbar.update() |
| 263 | + self.queue = [] |
| 264 | + |
| 265 | +# Necessary to define here to make the |
| 266 | +# function picklable |
| 267 | +def task_execute(task): |
| 268 | + task.execute() |
0 commit comments