Threads in Python version 1.5, thread doesn't start until calling process dies 
Author Message
 Threads in Python version 1.5, thread doesn't start until calling process dies

I am having some trouble with a threadded process in python version
1.5.  It seems that the thread does not start until the calling
process has died.  The thread starts immediately when the following
program is run under Python version 2.1.

**********************************************
import thread, time

def Thread1():
    while (1):
        print "I`m in the thread"
        time.sleep(1)

thread.start_new_thread(Thread1, ())
print "about to call time.sleep(4)"
time.sleep(4)
print "just slept for 4 seconds"
**********************************************

This is the Python version 1.5 execution of the above code:

about to call time.sleep(4)
just slept for 4 seconds
I`m in the thread
I`m in the thread
I`m in the thread
I`m in the thread
I`m in the thread
I`m in the thread

and here is the Python version 2.1 execution of the same code:

about to call time.sleep(4)
I`m in the thread
I`m in the thread
I`m in the thread
I`m in the thread
just slept for 4 seconds
I`m in the thread
I`m in the thread
I`m in the thread
I`m in the thread
I`m in the thread
I`m in the thread

Does any one have any suggestions?

Thanks in advance for your assistance.

Ken Tabacchi



Sun, 11 Jan 2004 22:01:20 GMT  
 Threads in Python version 1.5, thread doesn't start until calling process dies

That does look odd. What operating system are you using? And when you say
1.5, do you mean 1.5 or 1.5.2?

Quote:

> I am having some trouble with a threadded process in Python version
> 1.5.  It seems that the thread does not start until the calling
> process has died.  The thread starts immediately when the following
> program is run under Python version 2.1.

[ Elided. ]


Sun, 11 Jan 2004 23:44:11 GMT  
 Threads in Python version 1.5, thread doesn't start until calling process dies

Quote:

> I am having some trouble with a threadded process in Python version
> 1.5.  It seems that the thread does not start until the calling
> process has died.  The thread starts immediately when the following
> program is run under Python version 2.1.

> **********************************************
> import thread, time

> def Thread1():
>     while (1):
>         print "I`m in the thread"
>         time.sleep(1)

> thread.start_new_thread(Thread1, ())
> print "about to call time.sleep(4)"
> time.sleep(4)
> print "just slept for 4 seconds"
> **********************************************

> This is the Python version 1.5 execution of the above code:

> about to call time.sleep(4)
> just slept for 4 seconds
> I`m in the thread
> I`m in the thread
> I`m in the thread
> I`m in the thread
> I`m in the thread
> I`m in the thread

> and here is the Python version 2.1 execution of the same code:

> about to call time.sleep(4)
> I`m in the thread
> I`m in the thread
> I`m in the thread
> I`m in the thread
> just slept for 4 seconds
> I`m in the thread
> I`m in the thread
> I`m in the thread
> I`m in the thread
> I`m in the thread
> I`m in the thread

> Does any one have any suggestions?

Although the first output is unlikely, this might happen when
the python process gets no more CPU time after starting the
4 second sleep().

However, you should never rely on the process/thread scheduler(s)
for the correct behaviour of your program.
In case you want threads to wait for each other in any
reliable way, have a look at the threading module for inspiration.

Quote:

> Thanks in advance for your assistance.

> Ken Tabacchi

My pleasure,
Ype

--
email at xs4all.nl



Mon, 12 Jan 2004 04:00:19 GMT  
 Threads in Python version 1.5, thread doesn't start until calling process dies


Quote:
> I am having some trouble with a threadded process in Python version
> 1.5.  It seems that the thread does not start until the calling
> process has died.  The thread starts immediately when the following
> program is run under Python version 2.1.

Interesting one (for me, anyways).

Please forgive my ignorance: would I be correct to surmise that 1.5
lacks 'threading' and locks?



Mon, 12 Jan 2004 08:31:11 GMT  
 Threads in Python version 1.5, thread doesn't start until calling process dies

Quote:


> > I am having some trouble with a threadded process in Python version
> > 1.5.  It seems that the thread does not start until the calling
> > process has died.  The thread starts immediately when the following
> > program is run under Python version 2.1.

> Interesting one (for me, anyways).

> Please forgive my ignorance: would I be correct to surmise that 1.5
> lacks 'threading' and locks?

The 'threading' module was introduced in Python 1.5.1.  But the
'thread' module is much older, dating back to 0.9.8 at least.  The
thread module supports locks, too.

I don't know what to do about the original caller's problem, but on
some operating systems it helps to put a time.sleep(0.001) in the main
program after forking another thread, to allow the other thread to be
scheduled.

--Guido van Rossum (home page: http://www.python.org/~guido/)



Mon, 12 Jan 2004 22:03:14 GMT  
 Threads in Python version 1.5, thread doesn't start until calling process dies
Sorry about the omission of key information.  My system is Windows NT
4.0.  The Python interpreter is version 1.5.1, and, if this means
anything, it is the interpreter that came with a dSPACE Control Desk
version 1.1 package.

Something interesting I discovered this morning, my implementation of
the thread module works as expected when run in Pythonwin (1.5.1), but
gives the erroneous result (thread doesn't start until process that
called it has died) when run in the Control Desk environment.  I'm not
sure about which interpreter Control Desk is using, although they site
1.1.2.8 in their Help, About menu, but I think that number means
something only to dSPACE people.  Both Pythonwin and Control Desk
versions report the following when sys.version is called:

   '1.5.1 (#0, Jan 27 1999, 11:10:46) [MSC 32 bit (Intel)]'

Same interpreter yet different result.  It has been suggested to use
the threading module for a more 'predictable' execution.  Does anyone
have any advice about simply starting a new thread, exiting a thread
and possibly acquiring a lock using the threading module?  Much
appreciated.

Quote:

> That does look odd. What operating system are you using? And when you say
> 1.5, do you mean 1.5 or 1.5.2?


> > I am having some trouble with a threadded process in Python version
> > 1.5.  It seems that the thread does not start until the calling
> > process has died.  The thread starts immediately when the following
> > program is run under Python version 2.1.
>  [ Elided. ]



Mon, 12 Jan 2004 22:03:38 GMT  
 Threads in Python version 1.5, thread doesn't start until calling process dies

Quote:

> Something interesting I discovered this morning, my implementation of
> the thread module works as expected when run in Pythonwin (1.5.1), but
> gives the erroneous result (thread doesn't start until process that
> called it has died) when run in the Control Desk environment.

I don't know anything about the environment, but is it just used to
execute an external Python application, or does it have its own
embedded interpreter from which it executes your scripts?

If the latter, and if there are any callbacks into the surrounding
Control Desk environment, then one question would be whether Control
Desk is written to be thread-friendly or not - perhaps it's not
relinquishing the global interpreter lock when working within its own
extensions and thus preventing any other Python threads from running.

Quote:
> Same interpreter yet different result.  It has been suggested to use
> the threading module for a more 'predictable' execution.  Does anyone
> have any advice about simply starting a new thread, exiting a thread
> and possibly acquiring a lock using the threading module?  Much
> appreciated.

I'm not sure that you're likely to get much different behavior from
threading than from thread, if only because it sounds like your issue
is lower level than that, but here's tiny example of starting up a
thread with the threading module.

I find it most convenient to subclass the Thread object rather than
using the generic class with a callable target, although either
approach will work.  You can create as many instances (threads) of
this object to compete.  I've used a simple mutex lock to match with
your thread usage, but the threading module has a variety of
synchronization primitives for other purposes as well.

Note that threading is one of a few modules built to be friendly for
"from xxx import *", and since it implements some fundamental
constructs that get used frequently in multi-threaded code, it's one
of the very few cases where I do use that form of import.

This sample defines a thread that loops, pausing for random intervals
from 0-1s, then grabs a lock, prints a message, and releases the lock.
Several such thread object instances are created to compete for the
lock.  An event object is used to signal from the main thread that the
threads should terminate.

    import time
    import random
    from threading import *

    # Any thread is created as an instance of the Thread class
    class MyThread(Thread):

        def __init__(self, threadname, lock, event):
            # You must call Thread.__init__ but can add other parameters
            # to this thread method - such as lock and event here.
            Thread.__init__(self,name=threadname)
            self.lock  = lock
            self.event = event

        def run(self):
            # This method is what runs when start() is called
            while 1:

                # Exit out if event triggers us to do so
                if self.event.isSet():
                    print '%s terminating' % self.getName()
                    return

                # Otherwise wait until we can get a lock
                self.lock.acquire()
                print '%s running' % self.getName()
                self.lock.release()
                time.sleep(random.random())

    if __name__ == "__main__":

        # Create the lock and event objects used for synchronization.
        # From the recent case sensitivity discussion, this exhibits
        # bad form as judged by about half of those reading this. :-)
        lock = Lock()
        event = Event()

        # Create 5 threads to compete
        threads = []
        for i in range(5):
            threads.append(MyThread('Thread %d' % i+1,lock,event))

        # Start them off
        for curthread in threads:
            curthread.start()

        # Do something in main thread if you like
        time.sleep(10)

        # Signal termination by activating the event
        event.set()

        # And then wait for the threads to terminate
        for curthread in threads:
            curthread.join()

--
-- David
--
/-----------------------------------------------------------------------\

  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 C{*filter*}Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



Tue, 13 Jan 2004 07:41:55 GMT  
 Threads in Python version 1.5, thread doesn't start until calling process dies

Quote:

>I am having some trouble with a threadded process in Python version
>1.5.  It seems that the thread does not start until the calling
>process has died.  The thread starts immediately when the following
>program is run under Python version 2.1.

There were a lot of thread bugs fixed between Python 1.5 and Python 2.0,
and I think a few more got fixed between 2.0 and 2.1.  I strongly
recommend that threaded applications use 2.0.1 or higher.
--

Hugs and backrubs -- I break Rule 6                 http://www.*-*-*.com/
Androgynous poly {*filter*} vanilla {*filter*} het Pythonista  

Fortune cookie: Watch your relations with other people carefully, be reserved.



Thu, 15 Jan 2004 01:54:14 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Thread#kill doesn't kill processes inside a thread

2. Threaded server doesn't run its threads

3. Python 1.4 does, Python 1.5 doesn't...

4. testers needed for ILU 2.0alpha12 + Python 1.5 + threads

5. ILU + Python 1.5 + threads

6. Help needed testing ILU support for Python 1.5 threads

7. Python 1.5 with threads on LINUX

8. ilu 2.0a12, Python 1.5, threading

9. Waiting until event is processed / Thread syncronization

10. FSU threads, Provenzano's threads, and python

11. Thread.new vs. Thread.start

12. Threaded Tcl calls non-thread-safe library functions

 

 
Powered by phpBB® Forum Software