【问题标题】:Python - import gtk in headless CentOS 6, causing the application execution failurePython - 在 headless CentOS 6 中导入 gtk,导致应用程序执行失败
【发布时间】:2013-11-21 22:50:20
【问题描述】:

我正在使用 import gtk , gobject 来运行多线程服务器(但实际上我不使用 GUI)

  • 在我的本地桌面上运行完美。
  • 但现在我尝试将其放入服务器,但失败如下:

我正在使用云 CentOS 服务器:

$ export DISPLAY=:0.0 && python /var/tmp/protocol/server.py
Traceback (most recent call last):
  File "/var/tmp/protocol/server.py", line 53, in <module>
    import gtk
  File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 64, in <module>
    _init()
  File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 52, in _init
    _gtk.init_check()
RuntimeError: could not open display

代码:

import gtk
import object

class server(object):

  def listener(self, sock, *args):
    conn, addr = sock.accept()
    gobject.io_add_watch(conn, gobject.IO_IN, self.handler)
    return True

  def handler(self, conn, *args):
    line = conn.recv(4096)
    ...

  def __init__(self):
    print "starting.."

  def run(self):
    """ Socket prepare """
    self.sock = socket.socket()
    self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    self.sock.bind(('', 7007))
    self.sock.listen(1)
    print "Listening..."
    gobject.io_add_watch(self.sock, gobject.IO_IN, self.listener)
    gtk.main() # import to let it run as 24/7

if __name__=='__main__':
    s=server()
    s.run()

编辑:试过了,但没用

  mainloop = gobject.MainLoop()
  s=server()
  s.run()
  mainloop.run()

找出问题所在:

$ python -m trace --trace server.py

__init__.py(634):         if self.lock:
__init__.py(635):             self.lock.release()
 --- modulename: threading, funcname: release
threading.py(137):         if self.__owner != _get_ident():
threading.py(139):         self.__count = count = self.__count - 1
threading.py(140):         if not count:
threading.py(141):             self.__owner = None
threading.py(142):             self.__block.release()
threading.py(144):                 self._note("%s.release(): final release", self)
 --- modulename: threading, funcname: _note
threading.py(65):             if self.__verbose:
__init__.py(682):         return rv
__init__.py(1217):             for hdlr in c.handlers:
__init__.py(1221):             if not c.propagate:
__init__.py(1224):                 c = c.parent
__init__.py(1216):         while c:
__init__.py(1225):         if (found == 0) and raiseExceptions and not self.manager.emittedNoHandlerWarning:
server.py(562):       return False

【问题讨论】:

  • gtk 是一个 GUI 工具包,不太适合构建服务器。有很多更合适的包。

标签: python gtk centos pygtk x11


【解决方案1】:

我会查看 glib 模块,尤其是 help(glib.MainLoop)。在幕后,gtk 在大多数事情上都使用 glib。你可以使用gobject、ios等。但是glib没有图形依赖。

【讨论】:

  • 你的意思是:loop = gobject.MainLoop() gobject.threads_init() context = loop.get_context() while 1: # Handle commands here ... context.iteration(True) ?
  • 也许你的意思是:self.timer = gobject.timeout_add(time_in_ms, self.remote.keepalive) ?
  • 您的主循环构造函数在没有运行代码的情况下看起来是正确的
  • 仍然无法正常工作。冻结在def handler(self, conn, *args):
猜你喜欢
  • 2023-02-02
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-20
  • 1970-01-01
  • 1970-01-01
  • 2015-11-28
相关资源
最近更新 更多