[Tutor] Threading in Python

Raymond Hettinger python@rcn.com
Wed, 27 Mar 2002 23:08:04 -0500


Hello Shiva,

Whenever Python hangs and a C++ extension is being used, it is good idea to
make a quick test to see whether the Python code or the C++ code is the
culprit.  Replace the call to the extension with some regular Python code,
say time.ctime( time.time() ) for example.  If the new code crashes, then
you've got a threading problem.  If not, then the C++ code may be flawed
(reference counting errors, bad pointer assignments, etc).

Once you've done that, post your code so we can take a look at it.


Raymond Hettinger



----- Original Message -----
From: "STAN" <stan@coolquiz.com>
To: <tutor@python.org>
Sent: Wednesday, March 27, 2002 4:46 AM
Subject: RE: [Tutor] Threading in Python


> Hi Tim,
>
> Hi,
>
> I tried it and its working. Thanks a lot.But there seems to be a problem.
After the thread's target prints a few lines, the interpreter seems to hang
(I have been using the IDLE Python GUI on Windows). I experimented with this
for a few times and the problem is pretty consistent. I have a feeling that
there may be a flaw in my management of the thread's memory, synchronization
etc.
>
> Can you please address the problem ? In the feature what I am trying to
do, I need to print atleast 8 lines of data - at random intervals over and
over. This problem is stopping me from doing this .....
>
> The data is returned to Python as char * from a C++ extension.
>
> Do you have a better solution to this problem ..
>
> Thanks and Regards
>   Shiva
>
> --- Tim Peters <tim.one@comcast.net> wrote:
> >[STAN]
> >> How is it possible to run a seperate process in the background
> >> using Python ?
> >
> >Sounds like you actually want a thread (!= process), and actually don't
mean
> >much of anything by "background" <wink>.
> >
> >> I have a main process that is console based, that gives the user
> >> a few options and acts according to their choice.
> >>
> >> But I want a process to be running in the background, which
> >> sleeps for x secs and keeps checking for some data [ Contacts a
> >> C++ extension].
> >>
> >> I tried thread.start_new_thread(...) and also the Thread class
> >> but was unsuccessful :
> >
> >The good news is that this is easy to fix.
> >
> >> This is what I tried ( a very simple and abstract simulation of
> >> what I intend to do)::
> >>
> >> *****************************************************
> >> from threading import *
> >> from random import Random
> >
> >You're missing an "import time" here.
> >
> >> def printResult():
> >> r = Random(68)
> >> while 1:
> >> time.sleep(0.5)   # Is there a wait method I can
> >>                             # use here for more efficiency ?
> >
> >What do you want it to wait *for*?  Not enough information.  Sleeping is
> >very efficient, btw, if the best answer you've got is "wait for half a
> >second".
> >
> >> x = r.random()
> >> if (x > 0.75):
> >
> >The parentheses aren't needed here, and are rarely seen in Python.
> >
> >> print "The Event has occured"
> >>
> >> # End of printResult()
> >>
> >>
> >> def startit():
> >> t = Thread(target = printResult(),args=())
> >                                     ^^
> >
> >The parentheses there are the cause of your problem:  you're *calling*
> >printResult here, and since printResult() never returns, neither does the
> >call to Thread (your main thread never gets beyond this line).  Delete
the
> >parentheses and you'll be much happier.
> >
> >> t.start()
> >> while 1:
> >> ch = raw_input("Continue ?")
> >> if (ch not in ('y',)):
> >> break
> >>
> >> # End of startit()
> >>
> >> >>> startit()
> >> ...
> >> I wanted printResult to be running in the background ..... But it
> >> is the only one that is running !!!
> >
> >That's explained above -- your main thread is waiting for the
printResult()
> >call to return; you never actually got so far as to create a second
thread.
> >But you're close:  remove the parens and you'll have two threads.
>
> _____________________________________________________________
> Cool Quiz - Websites That Keep You Guessing! http://www.coolquiz.com
>
> _____________________________________________________________
> Run a small business? Then you need professional email like
you@yourbiz.com from Everyone.net  http://www.everyone.net?tag
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>