开发者

PyGTK nonblocking

Is there a way to not block PyGTK while performing some expensive operation? 开发者_如何学编程I know threading would work but it would take a lot of code and setup.

Thanks,


Setting up a background thread in PyGTK is not all that difficult.


Threading is not really a good choice in pygtk. Just check the main loop for events in your expensive loop by doing, and let it do its thing:

while my_operation_running:

    <my code>

    while gtk.events_pending():
        gtk.main_iteration()

or tweak your expensive operation so it can be run step-by-step using

glib.idle_add(function)

Every time main loop doesn't have something to do, it will call "function", just make sure function keeps track of its progress and it makes only one iteration in each call.


Avoid threads, they don't work well at all with PyGTK. Some alternatives:

  • For IO/networking operations where you read from a file descriptor, use glib.io_add_watch()
  • Move the operations to a separate process and communicate with it using glib.spawn_async()

Twisted is a great library for doing networking, which is completely non-blocking and integrates well with the PyGTK mainloop, consider using that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜