Hello, Looking at the manuals, I saw that the function Py_NewInterpreter(), "simulates" the creation of a new thread, so that different scripts would be interpreted "pratically" in paralell by interpreting some bytecodes of each one from time to time. There is even a method to say how many bytecodes to interpret in one script (default=10) before changing to interpret the next script. I tryed to test this feature from C calls, by playing with thread states, interpreter states, run_string, etc, but with no success... What I would like to do is to have n scripts ( char *script[n]; ), and be able to run them in the same application loop, having something like : while ( true ) { for ( int i=0; i<n; i++ ) { restore_python_thread_state ( i ); // would call python functions to restore internal states, etc. run_some_bytecodes_of_python_script ( script[i] ); // would not block my loop, would not run all the script ! } .... } So, first of all, my question is : Is this possible to do ? If so, can someone explain me, or send me some example code please. Thank you all, Marcelo K. LIG - EPFL
On Mon, 15 Nov 1999, Marcelo Kallmann wrote:
... while ( true ) { for ( int i=0; i<n; i++ ) { restore_python_thread_state ( i ); // would call python functions to restore internal states, etc. run_some_bytecodes_of_python_script ( script[i] ); // would not block my loop, would not run all the script ! } .... }
So, first of all, my question is : Is this possible to do ?
Nope. Sorry. The Python interpreter runs to completion. It will not "unwind" and return to your loop. It uses *real* threads, and blocks on one to cause a context switch to another ready-thread. Well... more specifically, one thread grabs the global lock. Periodically, it releases it and re-acquires it. In that short time-frame when it doesn't hold the lock, another thread can wake up, grab it, and begin execution for a while. Cheers, -g -- Greg Stein, http://www.lyra.org/
The Python interpreter runs to completion. It will not "unwind" and return to your loop.
It uses *real* threads, and blocks on one to cause a context switch to another ready-thread.
Well... more specifically, one thread grabs the global lock. Periodically, it releases it and re-acquires it. In that short time-frame when it doesn't hold the lock, another thread can wake up, grab it, and begin execution for a while.
Thank you for the info. So, I would be able to do something like : char *scripts[N]; // my n scripts for ( i=0; i<n; i++ ) { start_and_interpret_in_new_thread ( scripts[i] ); } So, can someone explain me how to write such a function start_and_interpret_in_new_thread (s) ? Some example code would be apreciated . Thank you, Marcelo K. EPFL - DI - LIG
participants (2)
-
Greg Stein -
Marcelo Kallmann