thread execution order

Axel Mittendorf newsreply at transfertech.de
Thu Sep 30 09:33:06 EDT 2004


Hi, I have an app that writes text messages into a pipe and reads it out.
Other programs write into this pipe too. Sometimes the pipe is full and
my app freezes if it wants to write in that moment. It is not an option
for me to open the pipe with O_NONBLOCK, so I thought I could
use threads. Always when something shall be written into the pipe a new
thread is created. These threads may hang on os.write, but they'll write out
the data ASAP.
See the example below (I substituted os.write with lock.acquire(1)).
But if the pipe was full, three threads were created (all hang on os.write)
and
now the pipe is emptied, which one will write its data at first? The threads
ordered by the time of thier creation (first no 1 then no 2,3,4 ... n) or
just one of them
(not ordered, maybe no4 then no1 and then no3)?
I ask this since the order of the messages is important.

# little example
import threading

class TO(object):
    def __init__(self):
        self.lock = threading.Lock()
        self.lock.acquire(0)
    def dowrite(self,s):
        self.lock.acquire(1) # this should be a blocking os.write
        print "dowrite:",s
        self.lock.release()
    def write(self,s):
        print "write:",s
        t = threading.Thread(target= self.dowrite,args=(s,))
        t.start()

to = TO()
to.write("one")
to.write("two")
to.write("three")
to.lock.release()

TIA, Axel Mittendorf





More information about the Python-list mailing list