Re: Threading in a python extension?



On Wed, 2006-21-06 at 22:53 +0100, Magnus Therning wrote:
> On Sun, Jun 18, 2006 at 12:32:52 +0200, Frederic Peters wrote:
> >Magnus Therning wrote:
> >
> >> Does anyone have any examples of multi-threade extensions written in
> >> Python?
> >
> >Nope but multi-thread is perhaps not necessary, PyGTK FAQ has a good
> >suggestion about using gobject.idle_add and python enumerators.  I
> >have been using this trick in a few pygtk apps to keep them
> >responsives while not using threads and have been very happy with it.
> >
> >  http://www.async.com.br/faq/pygtk/index.py?req=show&file=faq23.020.htp
> 
> Isn't there an error in that recipe?
> 
> my_task() will return a new generator each time it's called so the
> following code wouldn't do what's intended, right?
> 
>   def on_start_my_task_button_click(data):    
>       gobject.idle_add(my_task().next, data)
> 
> I think what's intended is:
> 
>   def on_start_my_task_button_click(data):    
>       task = my_task()
>       gobject.idle_add(task.next, data)
> 
> Or did I miss something?

You're on the right track, but missing something. The function which is
being passed in gobject.idle_add() is the generator's ".next()" method.
So *that* method will be called time and time again until it returns
false.

Consider this analog:

def non_generator_fn(data):
	if data.i == 0:
		# "...some work..."
	if data.i < 10:
		# "...do heavy work here..."
		data.i += 1
		yield True
	yield False

data.i = 0
gobject.idle_add(non_generator_fn, data)

In other words, at each iteration, the Python interpreter won't evaluate
"my_task().next()". Instead, when my_task().next is evaluated, a new
my_task generator *object* is created and its "next()" method is passed
to gobject.idle_add().

I don't know how that "data" argument in the example manages to make it
to the generator. But if that is indeed code from James Henstridge, it
probably works. He's a genius.

HTH,

-- 
Adam Hooper <adamh densi com>

Attachment: signature.asc
Description: This is a digitally signed message part



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