threads, idle sources, python



It appears I've run up against a deficiency in pyglib. Maybe there is a
work around? 

Take the case of trying to schedule an idle callback to run in another
thread's main loop. An illustration in C...

#include <glib.h>
#include <unistd.h>

GMainContext *gmain_context;
GMainLoop *main_loop;

gboolean idle_callback(gpointer data)
{
 g_print("idle_callback() back inside main thread...\n");
 g_main_loop_quit(main_loop);
 return FALSE;
}

gpointer thread2_entry(gpointer data)
{
 GSource *source;

 g_print("inside other thread...\n");

 source = g_idle_source_new();

 g_source_set_callback(source, (GSourceFunc) idle_callback, NULL, NULL);

 g_source_attach(source, gmain_context);

 g_source_unref(source);
}

int main(int argc, char **argv)
{
 g_thread_init(NULL);

 gmain_context = g_main_context_default();

 main_loop = g_main_loop_new(gmain_context, FALSE);

 g_thread_create(thread2_entry, NULL, FALSE, NULL);

 g_main_loop_run(main_loop);
 return 0;
}


main() creates a thread and runs a main loop. The new thread establishes
idle_callback to run in the first thread. 


Wouldn't it be great if this could be done in python?


import sys
import threading
import glib


glib.threads_init()

GMainContext = glib.MainContext()

MainLoop = glib.MainLoop(context=GMainContext)

def IdleCallback():
        print "Idle Callback() back inside main thread..."
        MainLoop.quit

def ThreadEntry():
        print "inside other thread..."
        glib.idle_add(IdleCallback, context=GMainContext)
 
Thread = threading.Thread(target=ThreadEntry)
Thread.start()

MainLoop.run()


Of course there is no "context" parameter to glib.idle_add(). So close yet
so far away...




-- 
www.thomasstover.com
FLYNN LIVES!



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]