Dec-24-2018, 09:09 AM
I am getting gevent LoopExit error when using paramiko based ssh client with flask server.
Sample code to reproduce the problem.
This didn't work for me, getting exact same error. Even tried to do monkeypatching in wsgi file like:
Any help will be appreciated.
Sample code to reproduce the problem.
#!usr/bin/python3.6 #from gevent import monkey; monkey.patch_all() from pssh.clients.native import SSHClient import flask from flask import request app = flask.Flask(__name__) app.config["DEBUG"] = False def checkcpu(ip): cmd_cpu_usage = '''awk '{u=$2+$4; t=$2+$4+$5; if (NR==1){u1=u; t1=t;} else print ($2+$4-u1) * 100 / (t-t1) "%"; }' <(grep 'cpu ' /proc/stat) <(sleep 1;grep 'cpu ' /proc/stat)''' server = SSHClient(ip,user='user',password='password') output = server.run_command(cmd_cpu_usage) status = '' for line in output[2]: status += line return status @app.route('/', methods=['GET']) def home(): return '<h1>_WORKS_<h1>' @app.route('/v1/check/cpu', methods=['GET']) def api_cpu_check(): if 'ip' in request.args: ip = request.args['ip'] else: return "Error: IP required" return checkcpu(ip)
Error:ERROR in app: Exception on /v1/check/cpu [GET]
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 35, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/var/www/az/test.py", line 33, in api_cpu_check
return checkcpu(ip)
File "/var/www/az/test.py", line 16, in checkcpu
server = SSHClient(ip,user='user',password='*********')
File "/usr/local/lib/python3.6/dist-packages/pssh/clients/native/single.py", line 130, in __init__
self._connect(self._host, self.port)
File "/usr/local/lib/python3.6/dist-packages/pssh/clients/native/single.py", line 209, in _connect
self.sock.connect((host, port))
File "/usr/local/lib/python3.6/dist-packages/gevent/_socket3.py", line 329, in connect
address = _socketcommon._resolve_addr(self._sock, address)
File "/usr/local/lib/python3.6/dist-packages/gevent/_socketcommon.py", line 376, in _resolve_addr
r = getaddrinfo(host, None, sock.family)
File "/usr/local/lib/python3.6/dist-packages/gevent/_socketcommon.py", line 208, in getaddrinfo
return get_hub().resolver.getaddrinfo(host, port, family, type, proto, flags)
File "/usr/local/lib/python3.6/dist-packages/gevent/resolver/thread.py", line 65, in getaddrinfo
return self.pool.apply(_socket.getaddrinfo, args, kwargs)
File "/usr/local/lib/python3.6/dist-packages/gevent/pool.py", line 159, in apply
return self.spawn(func, *args, **kwds).get()
File "src/gevent/event.py", line 381, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 406, in gevent._event.AsyncResult.get
File "src/gevent/event.py", line 117, in gevent._event._AbstractLinkable._wait_core
File "src/gevent/event.py", line 119, in gevent._event._AbstractLinkable._wait_core
File "src/gevent/_greenlet_primitives.py", line 59, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 59, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 63, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/__greenlet_primitives.pxd", line 35, in gevent.__greenlet_primitives._greenlet_switch
gevent.exceptions.LoopExit: This operation would block forever
\tHub: <Hub '' at 0x7f929d9a3828 epoll default pending=0 ref=0 fileno=18 resolver=<gevent.resolver_thread.Resolver at 0x7f9298eeacf8 pool=<ThreadPool at 0x7f929b9ae1d0 0/1/10 hub=<Hub at 0x7f929d9a3828 thread_ident=0x140268143462272>>> threadpool=<ThreadPool at 0x7f929b9ae1d0 0/1/10 hub=<Hub at 0x7f929d9a3828 thread_ident=0x140268143462272>> thread_ident=0x7f92b8dd4780>
\tHandles:
[]
This code works on development server on windows...But gives the above error with when run on linux server as wsgi application.This didn't work for me, getting exact same error. Even tried to do monkeypatching in wsgi file like:
#!/usr/bin/python3.6 from gevent import monkey; monkey.patch_all() import sys sys.path.insert(0,"/var/www/testapp/") from test import app as applicationI am not familiar with gevent.
Any help will be appreciated.