6

Gtk 是一个与 Python 绑定的 GUI 工具包。Gevent 是一个 Python 网络库,构建在 libevent(较新版本的 libev)和 greenlets 之上,允许在 greenlets 中使用网络函数而不会阻塞整个过程。

Gtk 和 gevent 都具有调度事件的阻塞主循环。如何集成它们的主循环,以便我可以在我的应用程序上接收网络事件和 UI 事件,而不会阻塞另一个?

天真的方法是在 Gtk 的主循环上注册一个空闲回调,当没有 Gtk 事件时调用它。在这个回调中,我们产生了 greenlet,所以网络事件可以发生,同时也给出了一个小的超时,所以进程不会忙等待:

from gi.repository import GLib
import gevent

def _idle():
    gevent.sleep(0.1)
    return True

GLib.idle_add(_idle)

这种方法远非理想,因为我在 UI 事件处理之间有 100 毫秒的延迟,如果我将值降低太多,我会浪费太多的处理器忙于等待。

我想要一种更好的方法,在没有事件要处理的情况下,我的进程真正处于睡眠状态。

PS:我已经找到了一个特定于 Linux 的解决方案(也可能在 MacOS 下工作)。我现在真正需要的是一个有效的 Windows 解决方案。

4

3 回答 3

8

鉴于当前的 gevent API,我认为没有通用的解决方案,但我认为每个平台可能都有特定的解决方案。

Posix解决方案(在Linux下测试)

由于 GLib 的主循环接口允许我们设置 poll 函数,即获取一组文件描述符并在其中一个准备好时返回的函数,我们定义了一个 poll 函数,它依赖于 gevent 的 select 来知道文件描述符何时准备好.

Gevent 没有暴露poll()接口,select()接口有点不同,所以我们在调用的时候要翻译参数和返回值gevent.select.select()

使事情复杂一点的是,GLib 没有通过 Python 的接口公开g_main_set_poll_func()允许该技巧的特定功能。所以我们必须直接使用C函数,为此,ctypes模块就派上用场了。

import ctypes
from gi.repository import GLib
from gevent import select

# Python representation of C struct
class _GPollFD(ctypes.Structure):
    _fields_ = [("fd", ctypes.c_int),
                ("events", ctypes.c_short),
                ("revents", ctypes.c_short)]

# Poll function signature
_poll_func_builder = ctypes.CFUNCTYPE(None, ctypes.POINTER(_GPollFD), ctypes.c_uint, ctypes.c_int)

# Pool function
def _poll(ufds, nfsd, timeout):
    rlist = []
    wlist = []
    xlist = []

    for i in xrange(nfsd):
        wfd = ufds[i]
        if wfd.events & GLib.IOCondition.IN.real:
            rlist.append(wfd.fd)
        if wfd.events & GLib.IOCondition.OUT.real:
            wlist.append(wfd.fd)
        if wfd.events & (GLib.IOCondition.ERR.real | GLib.IOCondition.HUP.real):
            xlist.append(wfd.fd)

    if timeout < 0:
        timeout = None
    else:
        timeout = timeout / 1000.0

    (rlist, wlist, xlist) = select.select(rlist, wlist, xlist, timeout)

    for i in xrange(nfsd):
        wfd = ufds[i]
        wfd.revents = 0
        if wfd.fd in rlist:
            wfd.revents = GLib.IOCondition.IN.real
        if wfd.fd in wlist:
            wfd.revents |= GLib.IOCondition.OUT.real
        if wfd.fd in xlist:
            wfd.revents |= GLib.IOCondition.HUP.real
        ufds[i] = wfd

_poll_func = _poll_func_builder(_poll)

glib = ctypes.CDLL('libglib-2.0.so.0')
glib.g_main_context_set_poll_func(None, _poll_func)

我觉得应该有更好的解决方案,因为这样我们需要知道正在使用的 GLib 的具体版本/名称。g_main_set_poll_func()如果 GLib在 Python 中公开,则可以避免这种情况。此外,如果gevent实施select(),它可以很好地实施poll(),什么会使这个解决方案更简单。

Windows 部分解决方案(又丑又坏)

Posix 解决方案在 Windows 上失败,因为select()只能使用网络套接字,而给定的 Gtk 句柄则不是。所以我想在另一个线程中使用 GLib 自己的g_poll()实现(Posix 上的瘦包装器,它是 Windows 上相当复杂的实现)来等待 UI 事件,并通过 TCP 套接字将其与主线程中的 gevent 端同步. 这是一个非常难看的方法,因为它需要真正的线程(除了你可能会使用的greenlets,如果你正在使用gevent)和等待线程端的普通(非gevent)套接字。

太糟糕了 Windows 上的 UI 事件是由线程分割的,因此默认情况下一个线程不能等待另一个线程上的事件。在您执行一些 UI 操作之前,不会创建特定线程上的消息队列。所以我不得不MessageBoxA()在等待线程上创建一个空的 WinAPI 消息框()(当然有更好的方法),并用它来处理线程消息队列,AttachThreadInput()以便它可以看到主线程的事件。这一切都通过ctypes.

import ctypes
import ctypes.wintypes
import gevent
from gevent_patcher import orig_socket as socket
from gi.repository import GLib
from threading import Thread

_poll_args = None
_sock = None
_running = True

def _poll_thread(glib, addr, main_tid):
    global _poll_args

    # Needed to create a message queue on this thread:
    ctypes.windll.user32.MessageBoxA(None, ctypes.c_char_p('Ugly hack'),
                                     ctypes.c_char_p('Just click'), 0)

    this_tid = ctypes.wintypes.DWORD(ctypes.windll.kernel32.GetCurrentThreadId())
    w_true = ctypes.wintypes.BOOL(True)
    w_false = ctypes.wintypes.BOOL(False)

    sock = socket()
    sock.connect(addr)
    del addr

    try:
        while _running:
            sock.recv(1)
            ctypes.windll.user32.AttachThreadInput(main_tid, this_tid, w_true)
            glib.g_poll(*_poll_args)
            ctypes.windll.user32.AttachThreadInput(main_tid, this_tid, w_false)
            sock.send('a')
    except IOError:
        pass
    sock.close()

class _GPollFD(ctypes.Structure):
    _fields_ = [("fd", ctypes.c_int),
                ("events", ctypes.c_short),
                ("revents", ctypes.c_short)]

_poll_func_builder = ctypes.CFUNCTYPE(None, ctypes.POINTER(_GPollFD), ctypes.c_uint, ctypes.c_int)
def _poll(*args):
    global _poll_args
    _poll_args = args
    _sock.send('a')
    _sock.recv(1)

_poll_func = _poll_func_builder(_poll)

# Must be called before Gtk.main()
def register_poll():
    global _sock

    sock = gevent.socket.socket()
    sock.bind(('127.0.0.1', 0))
    addr = sock.getsockname()
    sock.listen(1)

    this_tid = ctypes.wintypes.DWORD(ctypes.windll.kernel32.GetCurrentThreadId())
    glib = ctypes.CDLL('libglib-2.0-0.dll')
    Thread(target=_poll_thread, args=(glib, addr, this_tid)).start()
    _sock, _ = sock.accept()
    sock.close()

    glib.g_main_context_set_poll_func(None, _poll_func)

# Must be called after Gtk.main()
def clean_poll():
    global _sock, _running
    _running = False
    _sock.close()
    del _sock

到目前为止,应用程序运行并正确响应点击和其他用户事件,但在窗口内没有绘制任何内容(我可以看到框架和粘贴到其中的背景缓冲区)。在线程和消息队列的处理中可能缺少一些重绘命令。我不知道如何解决它。有什么帮助吗?关于如何做到这一点有更好的主意吗?

于 2012-09-13T21:13:32.097 回答
1

或者您可以创建一个单独的线程来处理 Gevent 主循环。你需要一种机制来安全地从一个线程切换到另一个线程。

  1. 从 gui 线程安全地切换到 Gevent 线程(类似于 Stackless 已经提供的东西:在 Stackless Python 中的 diff OS 线程之间切换)。切换安全的解决方案是使用libev.ev_async

示例代码:

def in_gevent_thread(data_to_send):
  #now you are in gevent thread, so you can use safe greenlet + and network stuff
    ...

def on_button_click():
  #now you are in gui thread
  safe_switch = gevent.core.async(gevent_hub.loop)
  safe_switch.callback = functools.partial(in_gevent_thread, data_to_send)
  safe_switch.send()
  1. 从 gevent 线程切换回 gui 线程应该很容易。与 WinApi PostThreadMessage 类似的东西也应该在 gtk 中......
于 2012-09-21T23:54:45.537 回答
1

如果你使用 glibtimeout_add而不是idle_add对 CPU 成本的影响是最小的(我什至没有注意到任何)。这是 GTK2 + gevent 的完整示例:

import gtk
import gobject

import gevent
from gevent.server import StreamServer
from gevent.socket import create_connection


def handle_tcp(socket, address):
    print 'new tcp connection!'
    while True:
        socket.send('hello\n')
        gevent.sleep(1)


def client_connect(address):
    sockfile = create_connection(address).makefile()
    while True:
        line = sockfile.readline()  # returns None on EOF
        if line is not None:
            print "<<<", line,
        else:
            break


def _trigger_loop():
    gobject.timeout_add(10, gevent_loop, priority=gobject.PRIORITY_HIGH)


def gevent_loop():
    gevent.sleep(0.001)
    _trigger_loop()
    return False


tcp_server = StreamServer(('127.0.0.1', 1234), handle_tcp)
tcp_server.start()
gevent.spawn(client_connect, ('127.0.0.1', 1234))
gevent.spawn(client_connect, ('127.0.0.1', 1234))

_trigger_loop()
gtk.main()
于 2012-12-06T10:00:21.623 回答