Skip to content
This repository

Pull request gevent check #207

Merged
merged 3 commits into from 7 months ago

4 participants

Pedro Vicente Jean-Philippe Caruana Sergio Oliveira Lance Stout
Pedro Vicente

Sometimes gevent has been loaded as module but monkey patch has not been performed. (f.e: Pymongo is doing it)

With the patch I propose to check if system thread module has been "monkey patched" with gevent and then do the right import of Queue.

Also I have added a parameter on testall.py to check the testsuite with gevent enabled/disabled.

Time elapsed doing the test suite has been improved a lot (at my desktop from 30 to 10 seconds)

Jean-Philippe Caruana

+1

Sergio Oliveira

Definitely needing it! :100:

I'm getting the error NotImplementedError: gevent is only usable from a single thread. After applying this patch the error is gone.

My code is just like the registration example provided by SleekXMPP but running on a Django project.

The file is here: https://github.com/TracyWebTech/django-conversejs/blob/master/conversejs/register.py

Stack trace:

Exception in thread scheduler_thread:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/vagrant/.virtualenvs/colab/local/lib/python2.7/site-packages/sleekxmpp/xmlstream/xmlstream.py", line 1742, in _scheduler_thread
    self.scheduler.process(threaded=False)
  File "/home/vagrant/.virtualenvs/colab/local/lib/python2.7/site-packages/sleekxmpp/xmlstream/scheduler.py", line 135, in process
    self._process()
  File "/home/vagrant/.virtualenvs/colab/local/lib/python2.7/site-packages/sleekxmpp/xmlstream/scheduler.py", line 185, in _process
    self.schedule_lock.release()
  File "/usr/lib/python2.7/threading.py", line 142, in release
    raise RuntimeError("cannot release un-acquired lock")
RuntimeError: cannot release un-acquired lock

Exception in thread event_thread_0:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/vagrant/.virtualenvs/colab/local/lib/python2.7/site-packages/sleekxmpp/xmlstream/xmlstream.py", line 1623, in _event_runner
    event = self.event_queue.get(True, timeout=wait)
  File "/home/vagrant/.virtualenvs/colab/local/lib/python2.7/site-packages/gevent/queue.py", line 190, in get
    return waiter.get()
  File "/home/vagrant/.virtualenvs/colab/local/lib/python2.7/site-packages/gevent/hub.py", line 321, in get
    return get_hub().switch()
  File "/home/vagrant/.virtualenvs/colab/local/lib/python2.7/site-packages/gevent/hub.py", line 135, in get_hub
    raise NotImplementedError('gevent is only usable from a single thread')
NotImplementedError: gevent is only usable from a single thread

Sergio Oliveira

@legastero could you please take a look to see if it's ok to be merged?

Sorry to bother you :)

Lance Stout legastero merged commit 07284f3 into from September 12, 2013
Lance Stout legastero closed this September 12, 2013
Sergio Oliveira

Awesome!! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
13  sleekxmpp/util/__init__.py
@@ -18,7 +18,18 @@
18 18
 # Standardize import of Queue class:
19 19
 
20 20
 import sys
21  
-if 'gevent' in sys.modules:
  21
+
  22
+def _gevent_threads_enabled():
  23
+    if not 'gevent' in sys.modules:
  24
+        return False
  25
+    try:
  26
+        from gevent import thread as green_thread
  27
+        thread = __import__('thread')
  28
+        return thread.LockType is green_thread.LockType
  29
+    except ImportError:
  30
+        return False
  31
+
  32
+if _gevent_threads_enabled():
22 33
     import gevent.queue as queue
23 34
     Queue = queue.JoinableQueue
24 35
 else:
10  testall.py
... ...
@@ -1,7 +1,11 @@
1 1
 #!/usr/bin/env python
2 2
 
3  
-import os
4 3
 import sys
  4
+if len(sys.argv)>1 and sys.argv[1].lower() == 'gevent':
  5
+    from gevent import monkey
  6
+    monkey.patch_all()
  7
+
  8
+import os
5 9
 import logging
6 10
 import unittest
7 11
 import distutils.core
@@ -57,7 +61,7 @@ def run(self):
57 61
 
58 62
 if __name__ == '__main__':
59 63
     result = run_tests()
60  
-    print("<tests %s ran='%s' errors='%s' fails='%s' success='%s' />" % (
  64
+    print("<tests %s ran='%s' errors='%s' fails='%s' success='%s' gevent_enabled=%s/>" % (
61 65
         "xmlns='http//andyet.net/protocol/tests'",
62 66
         result.testsRun, len(result.errors),
63  
-        len(result.failures), result.wasSuccessful()))
  67
+        len(result.failures), result.wasSuccessful(),'gevent' in sys.modules))
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.