471,348 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Creating multiple threads

I have a scenario where I need to do multiple things and the flow is not synchronous.

I need one thread to be listening to a socket using the recv socket api. Other thread to be sending some messages to the server and another thread performing some local action.

Which would be better way of implementing it ...

Create three threads using
thread.start_new_thread(func1)
thread.start_new_thread(func2)
thread.start_new_thread(func3)

And then have a main thread which waits, until all the threads are done executing ?

or using threading.thread module.

Any tips would be greatly appreciated ?

Also is there some kind of signal that can be raised in threads that would stop the execution of all the threads, if it hits a certain condition

Thanks,
-I
Sep 5 '07 #1
5 5885
bartonc
6,596 Expert 4TB
I prefer using thread objects from the threading module (threading.thread). They work quite well and have thread-safe means of signaling, as in:
Event( )
A factory function that returns a new event object. An event manages a flag that can be set to true with the set() method and reset to false with the clear() method. The wait() method blocks until the flag is true.
For message passing, I use Queue objects for the Queue module (Queue.Queue).
The Queue module implements a multi-producer, multi-consumer FIFO queue. It is especially useful in threads programming when information must be exchanged safely between multiple threads. The Queue class in this module implements all the required locking semantics. It depends on the availability of thread support in Python.
[Note: chapter references are for version 4.4.]
Sep 6 '07 #2
I prefer using thread objects from the threading module (threading.thread). They work quite well and have thread-safe means of signaling, as in:...
All the examples I have seen for threading module has the same function doing the same operation with multiple threads. But in my case, I need to call three different functions and I am not sure if I can use the threading module

Am I missing something really basic here ?
Sep 7 '07 #3
bartonc
6,596 Expert 4TB
All the examples I have seen for threading module has the same function doing the same operation with multiple threads. But in my case, I need to call three different functions and I am not sure if I can use the threading module

Am I missing something really basic here ?
The "target" argument of the thread creation method can be any function that you wish. In that function you would loop until signaled by one means or another by the main thread. In this simple example, there is no loop at all:
Expand|Select|Wrap|Line Numbers
  1. >>> def FuncToRunAsThread():
  2. ...     print "I ran"
  3. ...     
  4. >>> import threading
  5. >>> t = threading.Thread(target=FuncToRunAsThread)
  6. >>> t.start()
  7. I ran
  8. >>> 
I'll work up a slightly more complex example for you shortly.
Sep 7 '07 #4
bartonc
6,596 Expert 4TB
The "target" argument of the thread creation method can be any function that you wish. In that function you would loop until signaled by one means or another by the main thread. In this simple example, there is no loop at all:
Expand|Select|Wrap|Line Numbers
  1. >>> def FuncToRunAsThread():
  2. ...     print "I ran"
  3. ...     
  4. >>> import threading
  5. >>> t = threading.Thread(target=FuncToRunAsThread)
  6. >>> t.start()
  7. I ran
  8. >>> 
I'll work up a slightly more complex example for you shortly.
Expand|Select|Wrap|Line Numbers
  1. import time
  2. import threading
  3.  
  4. def FuncToRunAsThread(event):
  5.     print "started at %f" %time.time()
  6.     while not event.isSet():
  7.         pass  #do some work until the mainthread sets the event
  8.     print "stopped at %f" %time.time()
  9.  
  10. if __name__ == "__main__":
  11.     thrdHandshake = threading.Event()
  12.     workerThread = threading.Thread(target=FuncToRunAsThread, args=(thrdHandshake,))
  13.     workerThread.start()
  14.     print workerThread.isAlive()
  15.     time.sleep(5)
  16.     thrdHandshake.set()  # Set the handshake flag that will terminate the thread
  17.     workerThread.join()  # wait here until the thread is actually terminated
  18.     print workerThread.isAlive()  # without the previous line, isAlive() will usually? be true
  19.     print "main thread terminated at %f" %time.time()
started at 1189154645.265000
True
stopped at 1189154650.359000
False
main thread terminated at 1189154650.359000
Sep 7 '07 #5
Thanks for the eg. Barton.
Sep 11 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Tumurbaatar S. | last post: by
5 posts views Thread by Bob Bins | last post: by
35 posts views Thread by keerthyragavendran | last post: by
5 posts views Thread by dk60 | last post: by
9 posts views Thread by =?Utf-8?B?YmJn?= | last post: by

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.