Python Flask, custom status of background process as response












1















I'm having trouble returning a custom response from a background process that takes a long time and its being called from a view function in Flask. I use the EventSource method from the html to display the status of the process to the user. The process is encapsulated in an object instanced in the view function and I need a way to signalize the status of the process.



The significant parts of the object are:



class MyObject:
self.status = 0
def signal_status(self):
yield self.status
def doSomeProcess(self):
self.status = 1
self.foo()
self.status = 2
self.bar()
self.status = 3
def process_call(self):
Thread(target=self.async_process, args=(app, data)).start()
def async_process(self, app, data):
with app.app_context():
self.doSomeProcess(data)


And from the routes:



myObj = MyObject()

@app.route('/process/<data>')
def process(data):
global myObject
myObject.process_call(data)
return render_template('process.html')


@app.route('/progress')
def progress():
global myObject
return Response(myObject.signal_status(), mimetype='text/event-stream')


Then I'm trying to reveive the code in js from the response like this:



<script>
var source = new EventSource("/progress");
source.onmessage = function(event) {
console.log(event.data);
}
</script>


However nothing appears in the console more than a 200 response message. I've tried casting to str the yield response from signal_status(). If I print the call to signal_status() the number is displayed normaly, so i don't think the threading is the issue.



Am I using the right approach to this issue, or Is there any other way I could display the status of the process in the client?










share|improve this question























  • Have you considered to use a distributed task queue like Celery? My gut feels like you should not implement asynch processing by your own, if something like celery can help you. I personally have collected some good experience with it!

    – Lennart Blom
    Nov 13 '18 at 21:22


















1















I'm having trouble returning a custom response from a background process that takes a long time and its being called from a view function in Flask. I use the EventSource method from the html to display the status of the process to the user. The process is encapsulated in an object instanced in the view function and I need a way to signalize the status of the process.



The significant parts of the object are:



class MyObject:
self.status = 0
def signal_status(self):
yield self.status
def doSomeProcess(self):
self.status = 1
self.foo()
self.status = 2
self.bar()
self.status = 3
def process_call(self):
Thread(target=self.async_process, args=(app, data)).start()
def async_process(self, app, data):
with app.app_context():
self.doSomeProcess(data)


And from the routes:



myObj = MyObject()

@app.route('/process/<data>')
def process(data):
global myObject
myObject.process_call(data)
return render_template('process.html')


@app.route('/progress')
def progress():
global myObject
return Response(myObject.signal_status(), mimetype='text/event-stream')


Then I'm trying to reveive the code in js from the response like this:



<script>
var source = new EventSource("/progress");
source.onmessage = function(event) {
console.log(event.data);
}
</script>


However nothing appears in the console more than a 200 response message. I've tried casting to str the yield response from signal_status(). If I print the call to signal_status() the number is displayed normaly, so i don't think the threading is the issue.



Am I using the right approach to this issue, or Is there any other way I could display the status of the process in the client?










share|improve this question























  • Have you considered to use a distributed task queue like Celery? My gut feels like you should not implement asynch processing by your own, if something like celery can help you. I personally have collected some good experience with it!

    – Lennart Blom
    Nov 13 '18 at 21:22
















1












1








1








I'm having trouble returning a custom response from a background process that takes a long time and its being called from a view function in Flask. I use the EventSource method from the html to display the status of the process to the user. The process is encapsulated in an object instanced in the view function and I need a way to signalize the status of the process.



The significant parts of the object are:



class MyObject:
self.status = 0
def signal_status(self):
yield self.status
def doSomeProcess(self):
self.status = 1
self.foo()
self.status = 2
self.bar()
self.status = 3
def process_call(self):
Thread(target=self.async_process, args=(app, data)).start()
def async_process(self, app, data):
with app.app_context():
self.doSomeProcess(data)


And from the routes:



myObj = MyObject()

@app.route('/process/<data>')
def process(data):
global myObject
myObject.process_call(data)
return render_template('process.html')


@app.route('/progress')
def progress():
global myObject
return Response(myObject.signal_status(), mimetype='text/event-stream')


Then I'm trying to reveive the code in js from the response like this:



<script>
var source = new EventSource("/progress");
source.onmessage = function(event) {
console.log(event.data);
}
</script>


However nothing appears in the console more than a 200 response message. I've tried casting to str the yield response from signal_status(). If I print the call to signal_status() the number is displayed normaly, so i don't think the threading is the issue.



Am I using the right approach to this issue, or Is there any other way I could display the status of the process in the client?










share|improve this question














I'm having trouble returning a custom response from a background process that takes a long time and its being called from a view function in Flask. I use the EventSource method from the html to display the status of the process to the user. The process is encapsulated in an object instanced in the view function and I need a way to signalize the status of the process.



The significant parts of the object are:



class MyObject:
self.status = 0
def signal_status(self):
yield self.status
def doSomeProcess(self):
self.status = 1
self.foo()
self.status = 2
self.bar()
self.status = 3
def process_call(self):
Thread(target=self.async_process, args=(app, data)).start()
def async_process(self, app, data):
with app.app_context():
self.doSomeProcess(data)


And from the routes:



myObj = MyObject()

@app.route('/process/<data>')
def process(data):
global myObject
myObject.process_call(data)
return render_template('process.html')


@app.route('/progress')
def progress():
global myObject
return Response(myObject.signal_status(), mimetype='text/event-stream')


Then I'm trying to reveive the code in js from the response like this:



<script>
var source = new EventSource("/progress");
source.onmessage = function(event) {
console.log(event.data);
}
</script>


However nothing appears in the console more than a 200 response message. I've tried casting to str the yield response from signal_status(). If I print the call to signal_status() the number is displayed normaly, so i don't think the threading is the issue.



Am I using the right approach to this issue, or Is there any other way I could display the status of the process in the client?







javascript python jquery flask response






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 18:27









jevisanjevisan

62




62













  • Have you considered to use a distributed task queue like Celery? My gut feels like you should not implement asynch processing by your own, if something like celery can help you. I personally have collected some good experience with it!

    – Lennart Blom
    Nov 13 '18 at 21:22





















  • Have you considered to use a distributed task queue like Celery? My gut feels like you should not implement asynch processing by your own, if something like celery can help you. I personally have collected some good experience with it!

    – Lennart Blom
    Nov 13 '18 at 21:22



















Have you considered to use a distributed task queue like Celery? My gut feels like you should not implement asynch processing by your own, if something like celery can help you. I personally have collected some good experience with it!

– Lennart Blom
Nov 13 '18 at 21:22







Have you considered to use a distributed task queue like Celery? My gut feels like you should not implement asynch processing by your own, if something like celery can help you. I personally have collected some good experience with it!

– Lennart Blom
Nov 13 '18 at 21:22














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287366%2fpython-flask-custom-status-of-background-process-as-response%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287366%2fpython-flask-custom-status-of-background-process-as-response%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values