Problems with threading in embedded Python

Nico Blodow nicoblodow at gmx.net
Fri Aug 17 23:54:08 CEST 2007


Hi all,

I hope this hasn't been brought up before, but if it did i missed it, so 
bear with me please :)
anyway, i'm trying to embed Python into a C program. A sample python 
syript i want to run is as follows:

---SNIP---
import time
import threading

go_on = 1
t = 0

def ProcessMessage():
  print "... ProcessMessage"

def Main():
  global go_on
  print "... Main"
  while go_on:
    ProcessMessage ()
    print "...  ... ",
    time.sleep (0.2)

def Setup():
  global t
  print "... Setup"
  t = threading.Thread (None, Main)
  t.start ()
#  t.join (0)
#  print t.isAlive()

def Shutdown():
  global go_on, t
  go_on = 0
  t.join ()
  print "... Shutdown"

---SNIP---

I call "Setup" from C, which should create a new thread which should 
enter the loop in Main. well, it doesn't :)

It prints "... Setup" and does nothing. when i call Shutdown from C, it 
prints "... Main" , followed by "... Shutdown", and exits...

So it seems to me the python interpreter wants to run the seperate 
thread, but doesn't get time from the scheduler (is that the thing about 
100 opcodes?)
 How can i handle this problem? Do I need to wrap all the Py_Stuff() 
into its own thread? do i need artificial python code that only serves 
the purpose of piling up enough opcodes so that the Main() thread gets 
eventually called?

I hope you can help me, if you need more code (f.ex. the C-code ), i 
will post more :)
(basically I modified the run_func.cc file from the docs, added 
PyEval_InitThreads() and monkeyed around with GIL and 
Acquire/ReleaseLock, but so far i had no luck :)

Cheers, Nico



More information about the Python-list mailing list