473,721 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with blocking portably on sockets and Queue?

Hi,

I have several threads communicating with each other using events
stored in Queues. Threads block on Queue.get() until somebody
publishes an event in thread's event queue.

I need to add support for sockets to the system. Thread needs to
unblock when:

- there is socket ready to be read, or
- there is event waiting in the queue

My first tought was to replace blocking on Queue.get() with blocking
on poll or select and dedicating file descriptors (created with os.pipe())
as an semaphore. Event publisher would write something to the write
end of the pipe when it puts an event to the queue, effectively
unblocking the receiver.

BUT I noticed that select.poll() is not available on Windows and
secondly Windows version of select.select() will accept only socket
descriptors.

What options do I have that are still portable also to Windows
platform?

---
Tero

Jul 18 '05 #1
2 3892
<posted & mailed>

Tero Saarni wrote:
Hi,

I have several threads communicating with each other using events
stored in Queues. Threads block on Queue.get() until somebody
publishes an event in thread's event queue.
So far so wonderful -- excellent architecture.

I need to add support for sockets to the system. Thread needs to
unblock when:

- there is socket ready to be read, or
- there is event waiting in the queue

My first tought was to replace blocking on Queue.get() with blocking
on poll or select and dedicating file descriptors (created with os.pipe())
as an semaphore. Event publisher would write something to the write
end of the pipe when it puts an event to the queue, effectively
unblocking the receiver.

BUT I noticed that select.poll() is not available on Windows and
secondly Windows version of select.select() will accept only socket
descriptors.

What options do I have that are still portable also to Windows
platform?


I think you could devote a thread specifically to the task of handling
sockets, only. That special thread would not block on any queue but
just on select; when its select shows that a socket is ready, you can
have the thread itself do the reading and post the appropriate special
message including the data read to the appropriate thread, or you could
pass the ready-for-reading socket object to the appropriate thread, or
the like. This needs a bit more care if the set of sockets to be
select'ed on changes with time; in this case, I think the socket-handler
thread would have to use a timeout on its select so the set of sockets
can be updated once in a while (either by another thread, in which
case you need a lock to protect the "set of sockets" shared data, or
by the socket-handling thread (SHT) itself in response to a queued
message -- the SHT would read non-blockingly from that queue periodically
when the select times out) -- or you could perhaps use a special
identifiable socket for the SHT to receive from other threads requests
to change the set of ('true') sockets to be selected on.
Alex

Jul 18 '05 #2
"Alex Martelli" <al***@aleax.it > wrote in message
news:9P******** *************@n ews2.tin.it...
<posted & mailed>

Tero Saarni wrote:
Hi,

I have several threads communicating with each other using events
stored in Queues. Threads block on Queue.get() until somebody
publishes an event in thread's event queue.


So far so wonderful -- excellent architecture.

I need to add support for sockets to the system. Thread needs to
unblock when:

- there is socket ready to be read, or
- there is event waiting in the queue

My first tought was to replace blocking on Queue.get() with blocking
on poll or select and dedicating file descriptors (created with os.pipe()) as an semaphore. Event publisher would write something to the write
end of the pipe when it puts an event to the queue, effectively
unblocking the receiver.

BUT I noticed that select.poll() is not available on Windows and
secondly Windows version of select.select() will accept only socket
descriptors.

What options do I have that are still portable also to Windows
platform?


I think you could devote a thread specifically to the task of handling
sockets, only. That special thread would not block on any queue but
just on select; when its select shows that a socket is ready, you can
have the thread itself do the reading and post the appropriate special
message including the data read to the appropriate thread, or you could
pass the ready-for-reading socket object to the appropriate thread, or
the like. This needs a bit more care if the set of sockets to be
select'ed on changes with time; in this case, I think the socket-handler
thread would have to use a timeout on its select so the set of sockets
can be updated once in a while (either by another thread, in which
case you need a lock to protect the "set of sockets" shared data, or
by the socket-handling thread (SHT) itself in response to a queued
message -- the SHT would read non-blockingly from that queue periodically
when the select times out) -- or you could perhaps use a special
identifiable socket for the SHT to receive from other threads requests
to change the set of ('true') sockets to be selected on.
Alex


Good idea, thanks! I will try dedicating a special thread for socket
handling, although breaking out of select() for modifying the set of
sockets gets a little tricky as you pointed out.

Momentarily I was even thinking that I could break out of select() by
sending a signal when I want to modify the set of sockets, but
as I was almost expecting, it appeared that also signal support is
quite limited on Windows platform.

I guess that fallback solution would be to learn Windows
sychronization primitives (wait functions etc) and write two separate
implementations of relevant parts of the system. With some work
that could probably be hidden behind matching interfaces and a
factory method choosing the implementation on the fly.

---
Tero

Jul 18 '05 #3

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

Similar topics

0
1447
by: Gonçalo Rodrigues | last post by:
Hi, I have a problem with threads and sockets. I'll try to describe the problem in words with pseudo-code. I've been working on a few classes to make it easier to work with threads. This framework, let us call it that, consists in two parts. The first part is just a basic thread class deriving from threading.Thread with a few extra functionality that makes it easier for a thread to spawn a new thread and "father it". Each of its
6
3249
by: Tom | last post by:
I try to write a simple tcp based job queue server. It's purpose is to get commands from a client and store them in a queue (STL). The server has a thread which checks periodically the queue, executes the commands in it and removes them from the queue after done so. It also has a thread for every client connection. I am using the low level SOCKET API. My server is a Win32 console app and my client is MFC. The followinf struct is passed...
7
2866
by: David Sworder | last post by:
Hi, I'm developing an application that will support several thousand simultaneous connections on the server-side. I'm trying to maximize throughput. The client (WinForms) and server communicate via a socket connection (no remoting, no ASP.NET). The client sends a message to the server that contains some instructions and the server responds in an asynchronous fashion. In other words, the client doesn't block while waiting for the...
3
2491
by: Matthew King | last post by:
Hi all I've written a asynchronous socket client class, but i've found that in order to consume it I have to use events, and cannot simply for example SocketClient client = new SocketClient(110, "some.server.com") client.Connect() client.SendData("Hello World") Instead I have to wait for the async method to raise a Connected event, and call client.SendData from there, for complex chains of operations this because a nightmare chain of...
15
2209
by: mrpolitics | last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd) and everything was fine untill yesterday when the server crashed. So I did a cold restart and staretd the server back up now it's throwing this stream of errors right away. DOes anyone have any idea what they mean. I havn't changed the source at all since it was working (which was for four days). Unhandled Exception: System.TypeInitializationException: The type...
0
1304
by: Ben | last post by:
I modified the logmonitor sdk example so it would work over a network. It works great when the client and server are running on the same PC and have administrator privileges. So I have two problems: 1) I cannot run the client without administrator privileges. If I try I get the following message: Unhandled Exception: System.Security.SecurityException: Requested registry access is not allowed. at...
1
2825
by: opi | last post by:
My blocking TCP server hangs in the Accept method when the client software sometimes gets an error. Are there any ways to stop this blocking in the Accept method so I dont have to restart the server? I would rather not use threading or non-blocking sockets. Using VB.Net 2005.
1
20634
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but I don't see the error): "System.IO.IOException: Unable to read data from the transport connection:A blocking operation was interrupted by a call to WSACancelBlockingCall"
11
12465
by: Krzysztof Retel | last post by:
Hi guys, I am struggling writing fast UDP server. It has to handle around 10000 UDP packets per second. I started building that with non blocking socket and threads. Unfortunately my approach does not work at all. I wrote a simple case test: client and server. The client sends 2200 packets within 0.137447118759 secs. The tcpdump received 2189 packets, which is not bad at all. But the server only handles 700 -- 870 packets, when it is...
0
8840
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9064
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8007
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3189
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2130
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.