472,145 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Problems with threading in embedded Python

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
Aug 17 '07 #1
1 1351
In article <ma***************************************@python. org>,
Nico Blodow <ni********@gmx.netwrote:
>
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:
Looks like nobody here wants to touch this, maybe try
http://mail.python.org/mailman/listinfo/capi-sig
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"If you don't know what your program is supposed to do, you'd better not
start writing it." --Dijkstra
Aug 23 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.