473,241 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

embed threaded python code, thread won't start right the way

I have a multi thread python code, threads can start immediately if I run on command line, but I can get them started right the way if I call the same code from C/C++.

test code like this:
from threading import Thread
import thread
class testThread(Thread):
def __init__ (self, id):
Thread.__init__(self)
self.id = id
def run(self):
print " >>thread ", self.id, " started"
j = 0
for i in range(1, 2000):
j = j+1
print " <<< thread ", self.id, " ended"

def run():
t1 = testThread(1);
t2 = testThread(2);
t3 = testThread(3);
print "start t1"
t1.start();
print "start t2"
t2.start();
print "start t3"
t3.start();

If run this from command line, I get result immediately:
start t1
>>thread 1 started
<<< thread 1 ended
start t2
start t3
return from run() call
>> >>thread 2 started
<<< thread 2 ended
>>thread 3 started
<<< thread 3 ended

If I call this py code from c as:
Py_Initialize();
PyEval_InitThreads();
PyRun_SimpleString("import ****\n****.run()\n");
for (j=0; j<3; j++)
{
for (i=0; i<1000000; i++)
{
}
PyRun_SimpleString("print 'kick python'\n");
sleep(1);
printf("sleep\n");
}
printf("---------------- before Finalize()\n");
Py_Finalize();
printf("---------------- after Finalize()\n");

When c code is doing busy loop or sleeping, python thread can not run. they can only be executed when Py_Finalize(); is called. out put is like:
start t1
start t2
>>thread 1 started
start t3
return from run() call
kick python
sleep
kick python
sleep
kick python
sleep
---------------- before Finalize()
>>thread 2 started
thread 3 started
<<< thread 3 ended
<<< thread 2 ended
<<< thread 1 ended
---------------- after Finalize()


__________________________________________________ __________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
Dec 20 '07 #1
0 1500

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Paul Moore | last post by:
I can't find anything which spells this out in the manuals. I guess that, at some level, the answer is "a single bytecode operation", but I'm not sure that explains it for me. This thought was...
0
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to...
1
by: Eric S. Johansson | last post by:
at this point, I am looking more for a "is this practical/what will I get" response than an actual how to do it. I'm looking at putting python into emailrelay. Internally, it's POSIX threaded...
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
0
by: Mustafa Demirhan | last post by:
Hello, I am trying to add Python scripting support to one of my applications and I really need some help regarding the thread terminations. The application is a multi-threaded Windows...
1
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm...
3
by: Giampaolo Rodola' | last post by:
Hi, I'm trying to run an asynchronous FTP server I wrote into a thread for being able to run a test suite against it. The code below is the threaded FTP server code I'm using: --- snippet ---...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.