471,344 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Python Threads+execute

Hello to everyone
I wont to create 2 threads in Python ( thread01, thread02 ), which doing the follows :
thread01, ask user to give a value to flag variable and write it to the shared memory of two threads
thread02, read from shared memory and check if the flag have a specific value .
The corresponding python code follows:
Expand|Select|Wrap|Line Numbers
  1. import time
  2. import thread
  3. import threading
  4. import Numeric, sys 
  5.  
  6. class newThread(threading.Thread):
  7.  
  8.     def __init__(self,name,threadID,sleeptime):
  9.         self.name=name
  10.         self.threadID=threadID
  11.         self.sleeptime=sleeptime
  12.  
  13.     def run(self,flag):
  14.         print time.ctime(time.time())
  15.         if self.threadID==2:
  16.             print "%s flag : "%(self.name,flag)
  17.             while flag!=100:
  18.                 time.sleep(self.sleeptime)
  19.             sys.exit()
  20.         elif self.threadID==1:
  21.             while 1:
  22.                 print "%s flag:"%(self.name, flag)
  23.                 flag=int(raw_input("Give a value at flag: "))
  24.                 time.sleep(self.sleeptime)
  25. flag=0
  26.  
  27. thread1=newThread("Thread No:1",1,5)
  28. thread2=newThread("Thread No:2",2,15)
  29.  
  30. thread1.run(flag)
  31. thread2.run(flag)
  32.  
  33. while 1:
  34.     pass
  35.  
  36. thread1.exit()
  37. thread2.exit()
When I call it from python 2.5(Linux kubuntu 7.04), thread02 never take the CPU and only thread01 execute. Why does this happen ? The 2 threads does not run concurrently in double core processors?Why does not thread02 take cpu when thread 1 sleep? the Is there any solution of this problem?
Sep 24 '07 #1
5 3529
bartonc
6,596 Expert 4TB
First, let's get you using [code] tags. Instructions are on the right hand side of the page when you "start a discussion" with the heading "POSTING GUIDELINES" or when you reply, with the heading "REPLY GUIDELINES". Thanks.

I whipped up an example a few days ago. I generally use the simpler method that doesn't require subclassing of threading.thread. I'll look for that post and get back soon.
Sep 24 '07 #2
bartonc
6,596 Expert 4TB
For an example in interlocking threads from the main thread, see this thread.
For concurrency, see Parallel Python.

I wouldn't try to get user interaction from a thread that is not the main thread. Threads do tend to print fairly reliably, though.

sleep() is not guaranteed to release the processor. In some systems it works, but I've seen it fail.
Sep 24 '07 #3
Hello bartonc
Thank you for your advices, I appreciate it much, but still I have some questions. Does Parallel Python solve problems for concurrent execution of threads? As I read, Parallel Python used to solve problems for parallelism between processors,like MPI does. But my problem refer to parallel execution of 2 threads at the same processor, which execute different functions. Threads use shared memory communication so both of them can read and write to the variable flag. If first thread write to the flag the value of 200 then the second thread, according to the check which execute must end the program. The only way to make this is to change periodically the flow control of CPU . At first time, first thread must have the control and then the second. Do you have any ideas . Is there any function except sleep() , which stop the execution of one thread and pass the flow control to the other.
Thanks a lot , again.
Sep 25 '07 #4
bartonc
6,596 Expert 4TB
Hello bartonc
Thank you for your advices, I appreciate it much, but still I have some questions. Does Parallel Python solve problems for concurrent execution of threads? As I read, Parallel Python used to solve problems for parallelism between processors,like MPI does. But my problem refer to parallel execution of 2 threads at the same processor, which execute different functions. Threads use shared memory communication so both of them can read and write to the variable flag. If first thread write to the flag the value of 200 then the second thread, according to the check which execute must end the program. The only way to make this is to change periodically the flow control of CPU . At first time, first thread must have the control and then the second. Do you have any ideas . Is there any function except sleep() , which stop the execution of one thread and pass the flow control to the other.
Thanks a lot , again.
They will run in a multi-threaded environment and share cpu time if they are will written. The main rule to remember is that

Threads must use thread-safe means of communication! They may not share main thread memory.

By using a Queue.Queue() threads may wait for one another in a cooprative way.
threading.Event() is another technique that will work. There are also Locks, RLocks and Semaphore Objects at your disposal.
Sep 25 '07 #5
Thank you a lot, your advice was helpful. If I find any other relative question , i will write again .
Sep 27 '07 #6

Post your reply

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

Similar topics

3 posts views Thread by Krisztian Kepes | last post: by
37 posts views Thread by Carlos Ribeiro | last post: by
3 posts views Thread by Travis Berg | last post: by
2 posts views Thread by Kenneth McDonald | last post: by
3 posts views Thread by Wolfram | last post: by
2 posts views Thread by Gabriel Genellina | last post: by
4 posts views Thread by Ognjen Bezanov | 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.