multithreading on Linux
Steve Holden
sholden at holdenweb.com
Tue Sep 5 10:56:35 EDT 2000
Duncan Grisby wrote:
>
> In article <slrn8r6r97.3gj.bbp at zen.via.ecp.fr>,
> Brieuc Jeunhomme <bbp at via.ecp.fr> wrote:
>
> >However, I have recently written a multithreaded app, and I can't
> >understand why I always get weird
> >'currentThread(): no current thread for 1026' when a thread terminates.
> >Probably I have missed something.
>
> This happens when you mix threads created with the thread module (or a
> C extension) with constructs in the threading module. You should use
> the threading module to create threads if you intend to use threading
> for anything else.
>
> Cheers,
>
> Duncan.
>
> --
> -- Duncan Grisby \ Research Engineer --
> -- AT&T Laboratories Cambridge --
> -- http://www.uk.research.att.com/~dpg1 --
Here's a threading sample by Jeff Blaine which I stashed away some time
ago as potentially useful:
Ripped straight from Aahz Maruch's post the other day and simplified
as more of a 'visual learning tool' demo. Added a few comments...
whatever...just posting in case someone finds this one more graspable
at a thread-beginner level like me.
#!/usr/local/bin/python
import time, threading, whrandom
class MyThread(threading.Thread):
""" Each thread picks a 'random' integer between 0 and 19 and reports
in once per second for that many seconds.
"""
def run(self):
iterations = whrandom.randint(0, 19)
for i in range(iterations):
print " ", self.getName(), "is reporting in"
time.sleep(1)
print self.getName(), "is DONE"
if __name__ == '__main__':
threadList = []
# Create 5 MyThread() threads
for i in range(5) :
thread = MyThread()
threadList.append(thread)
# Start all threads
for thread in threadList:
thread.start()
# As long as we have more than just the 'main' thread running, print out
# a status message
while threading.activeCount() > 1 :
print str(threading.activeCount()), "threads running including main"
time.sleep(1)
Hope this helps.
regards
Steve
--
Helping people meet their information needs with training and technology.
703 967 0887 sholden at bellatlantic.net http://www.holdenweb.com/
More information about the Python-list
mailing list