[Python-Dev] Twisted Isn't Specific (was Re: Trial balloon: microthreads library in stdlib)

Larry Hastings larry at hastings.org
Thu Feb 15 18:48:47 CET 2007


Martin v. Löwis wrote:
> Now, for these generalized event loops, it may not be possible anymore 
> to combine all event sources into a single blocking call.

Right, that's why my proposal assumed that each disparate event source 
would need its own thread.


> Ah, threads :-( It turns out that you need to invoke GetMessage in the
> context of the thread in which the window was created. In a different
> thread, you won't get any messages.

Oof!  I'm embarrassed to have forgotten that.  But that's not a fatal 
problem.  It means that on Windows the PerfectReactor must service the 
blocking GetMessage loop, and these other threads notify the 
PerfectReactor of new events by sending a message.  (Either that, or, it 
could poll GetMessage and its incoming event queue without ever 
blocking.  But that is obviously suboptimal.)  I think I've done this 
sort of thing before, in fact.

Of course, in the absence of any windows, the Windows PerfectReactor 
could fall back to a mutex.  Abstract this inside PerfectReactor and its 
event sources wouldn't notice the difference.

> Integrating with threads might be a solution in some cases, and a 
> problem in others. You can't assume it is a universal solution.

Universal?  Yeah, I doubt it too.  But perhaps it would be "good enough 
for nearly all cases".  In the cases where it wasn't, it could throw an 
"I can't listen to that type of event right now" exception, forcing you 
to fall back to preconfiguring your central reactor by hand.

Anyway, like many folks I'm hoping this whole conversation results in 
establishing basic standard duck-typed conventions--what other languages 
might call "interfaces"--for event senders and receivers.  In which case 
this non-universal threaded approach would simply be one of several to 
choose from.

I'd be interested to hear about other situations where threading would 
cause a problem.  My suspicion is that Windows is the hard one, and as 
I've shown that one is solvable.


Thanks for the thoughtful reply,


/larry/


More information about the Python-Dev mailing list