Threads on embedded Python

Chris Liechti cliechti at
Tue Jun 25 00:52:05 CEST 2002

"Ugo García" <plugin at> wrote in
news:1024957605.771843 at seux119: 

> Let see if I get something clear... I don't have any main loop in
> Python. I only have some functions that 'control' certains object in
> my operation. To be a little more clear, the task of the functions are
> for example, move an object in the world generated by a game with a
> little intelligence. For example: (a simple example)
>     - Wait until main character is in correct position
>     - Play a sound
>     - Move an enemy from pos X1,Y1 to X2,Y2
>     - Play an animation
>     - ....
>     - ....
> So, I don't have a main loop, only function like that (and other kind
> of functions) that a run as a thread to have all them running
> simultaneous. Is there other way to do that without having threads?

sure, it's called polling ;-)

and i think this one could be an iteresting read for you:
it's a good way to distribute CPU power to different 'microthreads' while 
having  them well separated. altough you will need polling as microthreads 
don't support blocking IO.

why not hanging it up the other way round? i think if you have the main 
loop in python the thing will get easier. you can then call back to your C 
module to do the screen updates etc.

> Do
> I have to have a main loop in order to have the threads running?

when you're in a C module the Global Interpreter Lock stopps all python 
activity util you enable it explicitly (Py_BEGIN_ALLOW_THREADS) or you 
return to python.
the GIL makes writing extensions to python peanuts...

> Perhaps running Python in a separate thread could be a good idea.

yes someone has to spend the CPU cycles...

> Thanks,
> --ugo
> "Chris Liechti" <cliechti at> escribió en el mensaje
> news:Xns9237EECAA9CC5cliechtigmxnet at
>> "Ugo García" <plugin at> wrote in
>> news:1024952585.111238 at seux119:
>> > Hi all. I have an applicattion who uses Python to control some
>> > processes. I've made a little script library to work with threads
>> > and each proccess is a Python thread. The main application make
>> > calls to Python in order to create these proccesses. The problem is
>> > that the proccess doesn't run unless I call Python to do something.
>> > Now in the main loop I have a line like this:
>> >     PyRun_SimpleString("dummy=1");
>> >
>> > This make Python the threads to run. The problem is that this is a
>> > little slow 'cause if I call Python from tha main application too
>> > many times the proccesses run well but not the main application,
>> > and if a make a big pause between calls the proccesses doesn't work
>> > at a good speed. Is posible to have some threads running in an
>> > embedded Python without making callings to it continuosly? It would
>> > be a better idea to have another thread who's only task is to call
>> > Python (a line that the one written before) in order to have the
>> > Python proccesses running?
>> why not run python in a separate thread (created by your app) and
>> doing 
> the
>> loop in python, like PyRun_SimpleString("while 1: mainloop()") or so?
>> you do have a main loop in python, don't you? otherwise you wouldn't
>> need python threads, right? :-)
>> you could possibly also release the GIL, but don't forget to grab it
>> again when accessing the python interpreter. i'm not sure about this
>> one, so i would try the above.
>> chris
>> --
>> Chris <cliechti at>

Chris <cliechti at>

More information about the Python-list mailing list