473,396 Members | 1,995 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,396 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 1510

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.