Python Flask, Handling Popen poll / wait / communicate without halting multi-threaded Python -



Python Flask, Handling Popen poll / wait / communicate without halting multi-threaded Python -

the code below executed on url (/new...) , assigns variables session cookie, used build display. illustration calls command using subprocess.popen.

the problem popen command called below typically takes 3 minutes - , subprocess.communicate waits output - during time other flask calls (e.g. user connecting) halted. have commented lines related other things i've tried without success - 1 using threading module , using subprocess.poll.

from app import app flask import render_template, redirect, session subprocess import popen, pipe import threading @app.route('/new/<number>') def new_session(number): get_list(number) #t = threading.thread(target=get_list, args=(number)) #t.start() #t.join() homecoming redirect('/') def get_list(number): #1 phone call jar string command = 'java -jar fetch.jar' + str(number) print "executing " + command stream=popen(command, shell=false, stdout=pipe) #while stream.poll() none: # print "stream.poll = " + str(stream.poll()) # time.sleep(1) stdout,stderr = stream.communicate() #do item splits , processing, left out brevity session['data'] = stdout.split("\r\n") homecoming

what's "better practice" handling situation correctly?

for reference, code run in python 2.7.8 on win32, including flask 0.10.1

first, should utilize work queue celery, rabbitmq or redis (here helpful hint).

then, define get_list function becomes :

@celery.task def get_list(number): command = 'java -jar fetch.jar {}'.format(number) print "executing " + command stream = popen(command, shell=false, stdout=pipe) stdout, stderr = stream.communicate() homecoming stdout.split('\r\n')

and in view, wait result :

@app.route('/new/<number>') def new_session(number): result = get_list.delay(number) session['data'] = result.wait() homecoming redirect('/')

now, doesn't block view! :)

python flask subprocess python-multithreading

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -