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

Discussion in 'Python' started by Yue Fei, Dec 20, 2007.

  1. Yue Fei

    Yue Fei Guest

    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
     
    Yue Fei, Dec 20, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Microsoft

    Thread won't start?

    Microsoft, Aug 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    490
    Kevin Spencer
    Aug 3, 2005
  2. Doug Kent

    thread won't start

    Doug Kent, Oct 15, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    473
    Doug Kent
    Oct 18, 2005
  3. Enigma Curry
    Replies:
    1
    Views:
    503
    Peter Hansen
    Mar 15, 2006
  4. =?Utf-8?B?cGxleDRy?=
    Replies:
    0
    Views:
    363
    =?Utf-8?B?cGxleDRy?=
    Nov 13, 2007
  5. shooshx
    Replies:
    2
    Views:
    333
Loading...

Share This Page