python - Program with flask-socketio and multiprocessing thorws 'LoopExit: This operation would block forever' -


first: absolute beginner in python, used write php before, if getting complitly wrong please let me know.

i writing app. should serve information via websockets. choosed flask-socketio this. in background want process data. because have app small decided against solution celery.

i have shortened code to:

# -*- coding: utf8 -*-  flask import flask, jsonify, abort, make_response, url_for, request, render_template flask.ext.socketio import socketio, emit multiprocessing import pool multiprocessing.managers import basemanager import time import os  def background_stuff(args):     while true:         try:             print args             time.sleep(1)         except exception e:             return e  thread = none _pool = none  app = flask(__name__) app.debug = true socketio = socketio(app)  @app.route('/', methods=['get']) def get_timers():     return 'timer'  if __name__=='__main__':     _pool = pool(1)     if os.environ.get('werkzeug_run_main') == 'true':         workers = _pool.apply_async(             func=background_stuff,             args=('do background stuff',),         )     socketio.run(app)     # app.run() 

when starting this, following messages:

python test/multitest.py  * running on http://127.0.0.1:5000/  * restarting stat background stuff exception in thread thread-2: traceback (most recent call last):   file "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner     self.run()   file "/usr/lib/python2.7/threading.py", line 763, in run     self.__target(*self.__args, **self.__kwargs)   file "/usr/lib/python2.7/multiprocessing/pool.py", line 336, in _handle_tasks     taskseq, set_length in iter(taskqueue.get, none):   file "/usr/lib/python2.7/queue.py", line 168, in     self.not_empty.wait()   file "/usr/lib/python2.7/threading.py", line 340, in wait     waiter.acquire()   file "gevent/_semaphore.pyx", line 112, in gevent._semaphore.semaphore.acquire (gevent/gevent._semaphore.c:3386)   file "/home/phil/work/ttimer/server/local/lib/python2.7/site-packages/gevent/hub.py", line 338, in switch     return greenlet.switch(self) loopexit: operation block forever  background stuff background stuff background stuff background stuff background stuff 127.0.0.1 - - [2015-09-30 00:06:23] "get / http/1.1" 200 120 0.001860 background stuff background stuff background stuff background stuff ^cprocess poolworker-1: process poolworker-1: traceback (most recent call last): traceback (most recent call last):   file "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap   file "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap     self.run()     self.run()   file "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run   file "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run     self._target(*self._args, **self._kwargs)     self._target(*self._args, **self._kwargs)   file "/usr/lib/python2.7/multiprocessing/pool.py", line 113, in worker   file "/usr/lib/python2.7/multiprocessing/pool.py", line 102, in worker     task = get()     result = (true, func(*args, **kwds))   file "/usr/lib/python2.7/multiprocessing/queues.py", line 376, in   file "test/multitest.py", line 14, in background_stuff keyboardinterrupt     time.sleep(1) keyboardinterrupt     return recv() keyboardinterrupt 

so background process working , answers http requests (127.0.0.1 - - [2015-09-30 00:06:23] "get / http/1.1" 200 120 0.001860). ignoring error because seems work not seem solution me. can tell doing wrong here?

if can't way can tell me why? learn , understand doing wrong.

i read monkepatching, suggested threw more or other errors. think better work on first error instead of blindly trying fixes.

python -v python 2.7.9 

greetings

update

i added 2 lines monkeypatching, got:

$python multitest2.py   ^cprocess poolworker-1: traceback (most recent call last):   file "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap     self.run()   file "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run     self._target(*self._args, **self._kwargs)   file "/usr/lib/python2.7/multiprocessing/pool.py", line 102, in worker     task = get()   file "/usr/lib/python2.7/multiprocessing/queues.py", line 376, in     return recv() keyboardinterrupt exception in thread thread-3: traceback (most recent call last):   file "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner     self.run()   file "/usr/lib/python2.7/threading.py", line 763, in run     self.__target(*self.__args, **self.__kwargs)   file "/usr/lib/python2.7/multiprocessing/pool.py", line 380, in _handle_results     task = get() keyboardinterrupt   * running on http://127.0.0.1:5000/  * restarting stat ^cprocess poolworker-1: traceback (most recent call last):   file "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap exception in thread thread-3: traceback (most recent call last):   file "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner     self.run()   file "/usr/lib/python2.7/threading.py", line 763, in run     self.__target(*self.__args, **self.__kwargs)   file "/usr/lib/python2.7/multiprocessing/pool.py", line 380, in _handle_results     task = get() keyboardinterrupt      self.run()   file "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run     self._target(*self._args, **self._kwargs)   file "/usr/lib/python2.7/multiprocessing/pool.py", line 102, in worker     task = get()   file "/usr/lib/python2.7/multiprocessing/queues.py", line 376, in     return recv() keyboardinterrupt background stuff failed start flash policy server: [errno 98] address in use: ('127.0.0.1', 10843) $do background stuff background stuff background stuff background stuff background stuff background stuff background stuff background stuff background stuff background stuff background stuff background stuff 

on start there no output @ all. after hittinc ctrl-c several times, background stuff output. continues until kill python process via sigkill

update 2

what expect see

 * running on http://127.0.0.1:5000/  * restarting stat background stuff background stuff background stuff 

right after running of script. before press ctrl-c nothing happening.

first of all, need aware version of flask-socketio using requires gevent, coroutine framework. using asynchronous coroutines of gevent multiprocessing pool strange combination. using gevent, make sense use gevent pool functionality consistent.

now regarding problem, think due not having standard library monkey patched @ stage. recommend add following lines @ top of script (above imports, make them lines 1 , 2):

from gevent import monkey monkey.patch_all() 

these ensure calls standard library things such threads, semaphores, etc. go gevent implementations.

update: tried example. original version, without monkey-patching, works fine me, not see loopexit error reported. adding monkey patching prevents background processes running, reported.

in case, converted script use gevent.pool , works reliably me. here edited script:

from flask import flask, jsonify, abort, make_response, url_for, request, render_template flask.ext.socketio import socketio, emit gevent.pool import pool import time import os  def background_stuff(args):     while true:         try:             print args             time.sleep(1)         except exception e:             return e  thread = none _pool = none  app = flask(__name__) app.debug = true socketio = socketio(app)  @app.route('/', methods=['get']) def get_timers():     return 'timer'  if __name__=='__main__':     _pool = pool(1)     workers = _pool.apply_async(         func=background_stuff,         args=('do background stuff',),     )     socketio.run(app) 

hope helps!


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -