Python Threads+execute

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nimitsis
    New Member
    • Aug 2007
    • 16

    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:
    [CODE=python]
    import time
    import thread
    import threading
    import Numeric, sys

    class newThread(threa ding.Thread):

    def __init__(self,n ame,threadID,sl eeptime):
    self.name=name
    self.threadID=t hreadID
    self.sleeptime= sleeptime

    def run(self,flag):
    print time.ctime(time .time())
    if self.threadID== 2:
    print "%s flag : "%(self.name,fl ag)
    while flag!=100:
    time.sleep(self .sleeptime)
    sys.exit()
    elif self.threadID== 1:
    while 1:
    print "%s flag:"%(self.na me, flag)
    flag=int(raw_in put("Give a value at flag: "))
    time.sleep(self .sleeptime)
    flag=0

    thread1=newThre ad("Thread No:1",1,5)
    thread2=newThre ad("Thread No:2",2,15)

    thread1.run(fla g)
    thread2.run(fla g)

    while 1:
    pass

    thread1.exit()
    thread2.exit()[/CODE]

    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?
    Last edited by bartonc; Sep 24 '07, 08:20 PM. Reason: Added [CODE=python][/CODE] tags.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    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.threa d. I'll look for that post and get back soon.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      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.

      Comment

      • nimitsis
        New Member
        • Aug 2007
        • 16

        #4
        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.

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by nimitsis
          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.

          Comment

          • nimitsis
            New Member
            • Aug 2007
            • 16

            #6
            Thank you a lot, your advice was helpful. If I find any other relative question , i will write again .

            Comment

            Working...