title: monkey-patching requests in django
body: <p>I am trying to make a django view make several asynchronous http requests and return combined data.</p>
  
  <p>Here's an example that works in a separate file:</p>
  
  <pre><code>import gevent
  from gevent import monkey
  monkey.patch_all()
  import requests
  
  
  results = {}
  processes = []
  def fill_results(url, position):
      print(url, position, 'starting')
      results[position] = {'url': url, 'response': requests.get(url)}
      print(url, position, 'ready')
  
  
  urls = ['http://www.accessgenealogy.com'] * 5
  
  
  for position, url in enumerate(urls):
      processes.append(gevent.spawn(fill_results, url, position))
  
  gevent.joinall(processes)
  print(results)
  </code></pre>
  
  <p>the output I get is this:</p>
  
  <pre><code>('http://www.accessgenealogy.com', 0, 'starting')
  ('http://www.accessgenealogy.com', 1, 'starting')
  ('http://www.accessgenealogy.com', 2, 'starting')
  ('http://www.accessgenealogy.com', 3, 'starting')
  ('http://www.accessgenealogy.com', 4, 'starting')
  ('http://www.accessgenealogy.com', 3, 'ready')
  ('http://www.accessgenealogy.com', 0, 'ready')
  ('http://www.accessgenealogy.com', 4, 'ready')
  ('http://www.accessgenealogy.com', 2, 'ready')
  ('http://www.accessgenealogy.com', 1, 'ready')
  {0: {'url': 'http://www.accessgenealogy.com', 'response': &lt;Response [200]&gt;}, 1: {'url': 'http://www.accessgenealogy.com', 'response': &lt;Response [200]&gt;}, 2: {'url': 'http://www.accessgenealogy.com', 'response': &lt;Response [200]&gt;}, 3: {'url': 'http://www.accessgenealogy.com', 'response': &lt;Response [200]&gt;}, 4: {'url': 'http://www.accessgenealogy.com', 'response': &lt;Response [200]&gt;}}
  </code></pre>
  
  <p>so the requests are indeed asychronous.</p>
  
  <p>When I paste the same code into a django 1.9 view, the <code>monkey.patch_all()</code> breaks django: </p>
  
  <pre><code>Traceback (most recent call last):
    File "/Users/1111/.virtualenvs/django_cpa/lib/python2.7/site-packages/gevent/greenlet.py", line 327, in run
      result = self._run(*self.args, **self.kwargs)
    File "/Users/1111/.virtualenvs/django_cpa/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
      fn(*args, **kwargs)
    File "/Users/1111/.virtualenvs/django_cpa/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
      self.check_migrations()
    File "/Users/1111/.virtualenvs/django_cpa/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 163, in check_migrations
      executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
    File "/Users/1111/.virtualenvs/django_cpa/lib/python2.7/site-packages/django/db/migrations/executor.py", line 20, in __init__
      self.loader = MigrationLoader(self.connection)
    File "/Users/1111/.virtualenvs/django_cpa/lib/python2.7/site-packages/django/db/migrations/loader.py", line 49, in __init__
      self.build_graph()
    File "/Users/1111/.virtualenvs/django_cpa/lib/python2.7/site-packages/django/db/migrations/loader.py", line 176, in build_graph
      self.applied_migrations = recorder.applied_migrations()
    File "/Users/1111/.virtualenvs/django_cpa/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
      self.ensure_schema()
    File "/Users/1111/.virtualenvs/django_cpa/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 52, in ensure_schema
      if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
    File "/Users/1111/.virtualenvs/django_cpa/lib/python2.7/site-packages/django/db/backends/base/base.py", line 229, in cursor
      self.validate_thread_sharing()
    File "/Users/1111/.virtualenvs/django_cpa/lib/python2.7/site-packages/django/db/backends/base/base.py", line 523, in validate_thread_sharing
      % (self.alias, self._thread_ident, thread.get_ident()))
  DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias 'default' was created in thread id 140735166436112 and this is thread id 4488713392.
  &lt;Greenlet at 0x10b8c54b0: wrapper(use_static_handler=True, settings=None, pythonpath=None, verbosity=1, traceback=False, addrport='8000', no_color=False, use_ipv6=False, use_threading=True, use_reloader=True, insecure_serving=False)&gt; failed with DatabaseError
  </code></pre>
  
  <p>what specifically should I patch to make this work without breaking django?</p>