【问题标题】:bottle gevent and threading: gevent is only usable from a single thread瓶子 gevent 和线程:gevent 只能从单个线程中使用
【发布时间】:2013-10-26 17:31:36
【问题描述】:

我有一个使用线程的 python 瓶应用程序。由于我使用的是monkey.patch,线程阻塞了应用程序的执行(一个线程触发的对话框阻塞了瓶子路由响应客户端,直到被关闭。)

这里的一个小研究表明我应该使用猴子补丁而不尝试修补线程:

# Patch python's threads with greenlets
from gevent import monkey
monkey.patch_all(thread=False)

这不会阻止我写的最小的example

但是在密集使用线程时会引发这些错误,使用像threading.setEvent()这样的方法
这是我得到的错误:

C:\Users\IEUser\downloadloft-localserver>python mainserver.py
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 551, in _
_bootstrap_inner
self.run()
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 753, in r
un
self.finished.wait(self.interval)
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 403, in w
ait
self.__cond.wait(timeout)
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 262, in w
ait
_sleep(delay)
  File "c:\users\admini~1\appdata\local\temp\easy_install-sch3hm\gevent-0.13.8-p
y2.7-win32.egg.tmp\gevent\hub.py", line 79, in sleep
switch_result = get_hub().switch()
  File "c:\users\admini~1\appdata\local\temp\easy_install-sch3hm\gevent-0.13.8-p
y2.7-win32.egg.tmp\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

Bottle v0.12-dev server starting up (using GeventSocketIOServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 551, in _
_bootstrap_inner
self.run()
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 753, in r
un
self.finished.wait(self.interval)
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 403, in w
ait
self.__cond.wait(timeout)
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 262, in w
ait
_sleep(delay)
  File "c:\users\admini~1\appdata\local\temp\easy_install-sch3hm\gevent-0.13.8-p
y2.7-win32.egg.tmp\gevent\hub.py", line 79, in sleep
switch_result = get_hub().switch()
  File "c:\users\admini~1\appdata\local\temp\easy_install-sch3hm\gevent-0.13.8-p
y2.7-win32.egg.tmp\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

这是 gevent.monkeypatch 的已知问题吗?有什么想法吗?

【问题讨论】:

    标签: python bottle gevent python-multithreading


    【解决方案1】:

    Bottle 应用程序是线程化的,因此您不能在 Bottle 路由中调用的任何函数中使用 gevent。

    为了帮助你,我需要推测一下你为什么使用线程。

    如果要加速你的瓶子网站,只需要使用cherrypy服务器:

    pip install cherrypy 
    

    (或者只是将cherrypy目录转储到您当前的目录中,它是一个纯Python服务器)

    然后以这种方式运行您的瓶子应用程序:

    bottle.run(server='cherrypy')
    

    如果是因为你想在不阻塞响应的情况下进行非阻塞调用(例如获取 URL),那么手动操作很容易:

    • 创建一个 Queue 对象(它是一个特殊的队列,可以在线程之间填充和弹出)。
    • 创建并运行具有无限 while 循环的线程,每次都会取消队列并执行操作。
    • 当您需要非阻塞调用时,将操作推送到队列并携带一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多