title: Flask + gevent + uwsgi - greenlet spawn cause 502 Bad Gateway
body: <p>I have an issue since I added gevent to my flask project: the page spawning greenlets always timeout / bad gateway.</p>
  
  <p>The part of code causing this issue is this: <a href="https://github.com/Kyria/LazyBlacksmith/blob/master/lazyblacksmith/views/ajax.py#L145-L221" rel="nofollow">https://github.com/Kyria/LazyBlacksmith/blob/master/lazyblacksmith/views/ajax.py#L145-L221</a> </p>
  
  <p>Here what i've tested:</p>
  
  <ul>
  <li>Running my flask app with <code>python app.py</code> : works fine with or without gevent/greenlets.</li>
  <li>Running behind uwsgi without gevent/greenlet : works fine.</li>
  <li>Running behind uwsgi with gevent/greenlet : fails.</li>
  </ul>
  
  <p>So, after searching for a while, reading uwsgi doc, i tried to add some stuff to my uwsgi conf :</p>
  
  <pre><code>loop = async
  async = 10
  greenlet = true
  ugreen = true
  </code></pre>
  
  <p>After restarting my uwsgi, i didn't have to wait for error, the Bad Gateway was really quick, but with an error in the log this time:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
      return self.wsgi_app(environ, start_response)
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
      response = self.make_response(self.handle_exception(e))
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
      reraise(exc_type, exc_value, tb)
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
      response = self.full_dispatch_request()
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
      rv = self.handle_user_exception(e)
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
      reraise(exc_type, exc_value, tb)
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
      rv = self.dispatch_request()
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
      return self.view_functions[rule.endpoint](**req.view_args)
    File "./lazyblacksmith/views/ajax.py", line 152, in get_price_and_tax
      crest = get_crest()
    File "./lazyblacksmith/utils/crestutils.py", line 17, in get_crest
      crest()
    File "./lazyblacksmith/utils/pycrest/eve.py", line 212, in __call__
      self._data = APIObject(self.get(self._endpoint), self)
    File "./lazyblacksmith/utils/pycrest/eve.py", line 156, in get
      res = self._session.get(resource, params=prms, headers=headers)
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/requests/sessions.py", line 480, in get
      return self.request('GET', url, **kwargs)
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/requests/sessions.py", line 468, in request
      resp = self.send(prep, **send_kwargs)
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/requests/sessions.py", line 576, in send
      r = adapter.send(request, **kwargs)
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/requests/adapters.py", line 370, in send
      timeout=timeout
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 559, in urlopen
      body=body, headers=headers)
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 345, in _make_request
      self._validate_conn(conn)
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 782, in _validate_conn
      conn.connect()
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/requests/packages/urllib3/connection.py", line 215, in connect
      conn = self._new_conn()
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/requests/packages/urllib3/connection.py", line 135, in _new_conn
      (self.host, self.port), self.timeout, **extra_kw)
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/requests/packages/urllib3/util/connection.py", line 66, in create_connection
      for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/gevent/_socketcommon.py", line 268, in getaddrinfo
      return get_hub().resolver.getaddrinfo(host, port, family, socktype, proto, flags)
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/gevent/resolver_thread.py", line 64, in getaddrinfo
      return self.pool.apply(_socket.getaddrinfo, args, kwargs)
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/gevent/pool.py", line 300, in apply
      return self.spawn(func, *args, **kwds).get()
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/gevent/event.py", line 284, in get
      result = self.hub.switch()
    File "/home/lazyblacksmith/LazyBlacksmith/env/local/lib/python2.7/site-packages/gevent/hub.py", line 592, in switch
      return greenlet.switch(self)
  MemoryError
  </code></pre>
  
  <p>I also tried to add the following in my uwsgi app conf :</p>
  
  <pre><code>gevent = 10
  gevent-monkey-patch = true
  limit-as = 512
  </code></pre>
  
  <p>And i tried to increase the timeout in nginx <code>uwsgi_read_timeout 300</code>, but it didn't change anything... </p>
  
  <p>My uwsgi conf is the following: </p>
  
  <pre><code># lazyblacksmith.ini file
  [uwsgi]
  chdir           = /home/lazyblacksmith/LazyBlacksmith
  home            = /home/lazyblacksmith/LazyBlacksmith/env/
  callable        = app
  wsgi-file       = app.py
  
  master          = true
  processes       = 10
  
  socket          = /home/lazyblacksmith/LazyBlacksmith/env/lazyblacksmith.socket
  chmod-socket    = 664
  vacuum          = true
  uid             = lazyblacksmith
  gid             = www-data
  </code></pre>
  
  <p>My nginx conf :</p>
  
  <pre><code>upstream lbflask {
      server unix:/home/lazyblacksmith/LazyBlacksmith/env/lazyblacksmith.socket;
  }
  
  # configuration of the server
  server {
      # the port your site will be served on
      listen      80;
      # the domain name it will serve for
      server_name site.com; # 
      charset     utf-8;
  
      location /static {
          alias /home/lazyblacksmith/LazyBlacksmith/lazyblacksmith/static;
          include  /etc/nginx/mime.types;
      }
  
      # Finally, send all non-media requests to the Django server.
      location / {
          uwsgi_pass  lbflask;
          include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
      }
  }
  </code></pre>
  
  <p>I don't find anything relevant anymore :/ I think i'll end up moving my gevent stuff in tasks (with celery), but i still want to understand what is my problem... </p>
  
  <p>For information:</p>
  
  <ul>
  <li>Debian 8</li>
  <li>uwsgi 2.0.7-debian </li>
  <li>nginx/1.6.2 </li>
  <li>python 2.7.9 </li>
  <li>gevent 1.1rc2</li>
  <li>greenlet 0.4.9</li>
  </ul>
  
  <p>Thank you !</p>