python - Show a progress bar for my multithreaded process -
i have simple flask web app make many http requests external service when user push button. on client side have angularjs app.
the server side of code (using multiprocessing.dummy):
worker = myworkerclass() pool = pool(processes=10) result_objs = [pool.apply_async(worker.do_work, (q,)) q in queries] pool.close() # close pool pool.join() # wait task finish errors = not all(obj.successful() obj in result_objs) # extract result successful task items = [obj.get() obj in result_objs if obj.successful()]
as can see i'm using apply_async
because want later inspect each task , extract them result if task didn't raise exception.
i understood in order show progress bar on client side, need publish somewhere number of completed tasks made simple view this:
@app.route('/api/v1.0/progress', methods=['get']) def view_progress(): return jsonify(dict(progress=session['progress']))
that show content of session variable. now, during process, need update variable number of completed tasks (the total number of tasks complete fixed , known).
any ideas how that? working in right direction?
i'have seen similar questions on this one i'm not able adapt answer case.
thank you.
for interprocess communication can use multiprocessiong.queue , workers can put_nowait
tuples progress information on while doing work. main process can update whatever view_progress reading until results ready.
a bit in example usage of queue, few adjustments:
in writers (workers) i'd use put_nowait
instead of put
because working more important waiting report working (but perhaps judge otherwise , decide informing user is part of task , should never skipped).
the example puts
strings on queue, i'd use collections.namedtuples more structured messages. on tasks many steps, enables raise resolution of progress report, , report more user.
Comments
Post a Comment