473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

thread execution order

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
Jul 18 '05 #1
9 1259
Axel Mittendorf wrote:
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.


I wouldn't do this with threads. I'd use select() or similar to see if
the pipe is writable and if it isn't, buffer internally.
Jul 18 '05 #2
"Rembrandt Q Einstein" wrote:
I wouldn't do this with threads. I'd use select() or similar to see if
the pipe is writable and if it isn't, buffer internally.

I thought about something like this too, but since I'm not allowed to
use some kinda timer to write out the pending data I can't it that way.
Therefor I wondered to use threads.

THX, Axel
Jul 18 '05 #3
On Thu, 30 Sep 2004 16:27:23 +0200, "Axel Mittendorf"
<ne*******@transfertech.de> declaimed the following in comp.lang.python:
"Rembrandt Q Einstein" wrote:
I wouldn't do this with threads. I'd use select() or similar to see if
the pipe is writable and if it isn't, buffer internally. I thought about something like this too, but since I'm not allowed to
use some kinda timer to write out the pending data I can't it that way.
Therefor I wondered to use threads.

Possibly using ONE writing thread, and a Python Queue -- since
you say the message order is significant.

{pseudo-code}

loop
msg = q.get() #block if no message
pipe.write(msg) #can also block if pipe full
Probably need to create the Queue with no limit on messages,
otherwise you're back to blocking in the main routine.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #4
Axel Mittendorf wrote:
"Rembrandt Q Einstein" wrote:
I wouldn't do this with threads. I'd use select() or similar to see if
the pipe is writable and if it isn't, buffer internally.


I thought about something like this too, but since I'm not allowed to
use some kinda timer to write out the pending data I can't it that way.
Therefor I wondered to use threads.


You don't need a timer. Just every time your program tries to write, it
first checks the pipe. If it can write, it does. If it can't, it
buffers until the next time it tries.
Jul 18 '05 #5
"Rembrandt Q Einstein" <hercules.rockefeller@springfield.??.us> wrote:
You don't need a timer. Just every time your program tries to write, it
first checks the pipe. If it can write, it does. If it can't, it
buffers until the next time it tries.

What if there is no "next time". The buffered message might be buffered
until
my not yet planned child can program perl (no way, tv is enough)
or Linux is sold in bottles ;-)

THX, Axel
Jul 18 '05 #6
Axel Mittendorf wrote:
"Rembrandt Q Einstein" <hercules.rockefeller@springfield.??.us> wrote:
You don't need a timer. Just every time your program tries to write, it
first checks the pipe. If it can write, it does. If it can't, it
buffers until the next time it tries.


What if there is no "next time". The buffered message might be buffered
until
my not yet planned child can program perl (no way, tv is enough)
or Linux is sold in bottles ;-)

THX, Axel


Does the pipe eventually close? Write the rest of the data then.

Otherwise, I like the idea of a single thread that reads a common buffer
and writes and blocks however much it wants.
Jul 18 '05 #7
>>>>> "Axel" == Axel Mittendorf <ne*******@transfertech.de> writes:

Axel> use threads. Always when something shall be written into the
Axel> pipe a new thread is created. These threads may hang on
Axel> os.write, but they'll write out the data ASAP.

Use single thread that feeds the pipe from messages it gets from
a Queue.Queue instance.

--
Ville Vainio http://tinyurl.com/2prnb
Jul 18 '05 #8
"Axel Mittendorf" <ne*******@transfertech.de> wrote in message news:<cj*************@news.t-online.com>...
"Rembrandt Q Einstein" wrote:
I wouldn't do this with threads. I'd use select() or similar to see if
the pipe is writable and if it isn't, buffer internally.

I thought about something like this too, but since I'm not allowed to
use some kinda timer to write out the pending data I can't it that way.
Therefor I wondered to use threads.

THX, Axel


Alex!

In simular situations I use one writer thread and Queue per
pipe/socket etc. Worker threads put messages into the queue and
continue. Writer thread gets messages from the queue and writes them
into the pipe. If pipe is full writer waits, but workers are
operational. Writer thread is alive as long as the pipe exists. Queue
is used as a buffer.

Good luck.
Lev.
Jul 18 '05 #9
"Axel Mittendorf" <ne*******@transfertech.de> wrote in message news:<cj*************@news.t-online.com>...
"Rembrandt Q Einstein" wrote:
I wouldn't do this with threads. I'd use select() or similar to see if
the pipe is writable and if it isn't, buffer internally.

I thought about something like this too, but since I'm not allowed to
use some kinda timer to write out the pending data I can't it that way.
Therefor I wondered to use threads.


You shouldn't need a timer to do it with select(). That's how I'd do it too.
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Daylor | last post by:
in win32 process , when u create new process,u have new main thread. i know,appDomain r logical procces,that exists in 1 win32 process. the q: is there way to create second appDomain (the...
8
by: Maurice LING | last post by:
Hi, I just have a simple question about threads. My classes inherits from threading.Thread class. I am calling threading.Thread.run() method to spawn a few threads to parallel some parts of my...
12
by: Ricardo Pereira | last post by:
Hello all, I have a C# class (in this example, called A) that, in its constructor, starts a thread with a method of its own. That thread will be used to continuously check for one of its...
0
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs...
7
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs...
5
by: admin | last post by:
ok This is my main. Pretty much it goes through each category and starts up 4 worker threads that then ask for groups to gether from. My problem is that when the thread gets done it keeps the...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
16
by: Paul Schwann | last post by:
Hi group, I am relatively new to C# (although I have a lot of programming excperience in other languages like Java and C). Currently I am searching for a solution to this problem: Suppose you...
7
by: Curious | last post by:
On Jun 10, 3:32 am, <s...@dailycoding.comwrote: Thanks! I'll use thread pool.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.