Pwnna
Pwnna

Reputation: 9538

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,

Upvotes: 1

Views: 763

Answers (3)

Johan Dahlin
Johan Dahlin

Reputation: 26524

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.

Upvotes: 0

Marin
Marin

Reputation: 1331

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.

Upvotes: 1

Mark
Mark

Reputation: 108567

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

Upvotes: 3

Related Questions