473,657 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

threads and socket question

Hi,

My setup is the following: I have socket s from which I want to read
and write. So I made the following set up:

There is a thread whose only job is to read. Any data read (from recv
call) is just passed to (some) Queue. This thread is "owned" by a
second thread waiting on a Queue for write requests. The thread just
pops these from the Queue, and calls the send method from the socket.
This thread also takes care of closing the socket or (possibly)
handling any exceptions raised due to socket operation.

So my question is: since I have two threads sharing the same socket,
even though one is only reading and the other does everything else, do
I have to watch out for any "concurrenc y" issues?

P.S: This is for learning experience. So it's of no use telling me
that I should learn Twisted :-) I may (eventually) get there, but at
the moment I feel more omfortable with working with plain blocking
sockets.

With my best regards,
G. Rodrigues
Jul 18 '05 #1
2 3277
Gon?alo Rodrigues <op*****@mail.t elepac.pt> wrote:
My setup is the following: I have socket s from which I want to read
and write. So I made the following set up:

There is a thread whose only job is to read. Any data read (from recv
call) is just passed to (some) Queue. This thread is "owned" by a
second thread waiting on a Queue for write requests. The thread just
pops these from the Queue, and calls the send method from the socket.
This thread also takes care of closing the socket or (possibly)
handling any exceptions raised due to socket operation.

So my question is: since I have two threads sharing the same socket,
even though one is only reading and the other does everything else, do
I have to watch out for any "concurrenc y" issues?

P.S: This is for learning experience. So it's of no use telling me
that I should learn Twisted :-) I may (eventually) get there, but at
the moment I feel more omfortable with working with plain blocking
sockets.


The first problem I can think of is the one that stopped me. Note the
code below... You can't close a blocked socket in python even from a
separate thread.

import unittest
import socket
import threading
import time

class SocketAcceptor ( threading.Threa d ):
def __init__( self, socket ):
threading.Threa d.__init__( self )
self.socket = socket
self.done = 0

def run( self ):
self.socket.bin d( ( "", 3424 ) )
self.socket.lis ten( 5 )
try:
child, ip = self.socket.acc ept()
except:
pass
self.done = 1

class SocketTester ( unittest.TestCa se ):
def testClose( self ):
ss = socket.socket()
acceptor_thread = SocketAcceptor( ss )
acceptor_thread .start()
time.sleep( 1 )
ss.close()
time.sleep( 1 )
self.assertEqua ls( acceptor_thread .done, 1 )

if __name__ == '__main__':
unittest.main()
Jul 18 '05 #2
On Mon, 01 Sep 2003 01:34:32 GMT, "Daniel T."
<po********@ear thlink.net> wrote:
Gon?alo Rodrigues <op*****@mail.t elepac.pt> wrote:
My setup is the following: I have socket s from which I want to read
and write. So I made the following set up:

There is a thread whose only job is to read. Any data read (from recv
call) is just passed to (some) Queue. This thread is "owned" by a
second thread waiting on a Queue for write requests. The thread just
pops these from the Queue, and calls the send method from the socket.
This thread also takes care of closing the socket or (possibly)
handling any exceptions raised due to socket operation.

So my question is: since I have two threads sharing the same socket,
even though one is only reading and the other does everything else, do
I have to watch out for any "concurrenc y" issues?

P.S: This is for learning experience. So it's of no use telling me
that I should learn Twisted :-) I may (eventually) get there, but at
the moment I feel more omfortable with working with plain blocking
sockets.


The first problem I can think of is the one that stopped me. Note the
code below... You can't close a blocked socket in python even from a
separate thread.


Thanks for this piece of info. My experiences with my setup confirm
it.

I really want to keep the socket-closing in the write thread so my
problem is reduced to be able to order to read thread to die. What I
chose to do is to have it periodically check some exit flag to know
when to exit. I believe this can be done by calling select with a
timeout before the recv blocking call. Back to the docs and
Python-experimenting-mode.

With my best regards,
G. Rodrigues
Jul 18 '05 #3

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

Similar topics

0
1443
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
5
4223
by: Russell Warren | last post by:
Does anyone know the scope of the socket.setdefaulttimeout call? Is it a cross-process/system setting or does it stay local in the application in which it is called? I've been testing this and it seems to stay in the application scope, but the paranoid side of me thinks I may be missing something... any confirmation would be helpful.
2
2176
by: Ronodev.Sen | last post by:
the way my program needs to go is -- 1) open a socket and listen on it 2) moment a client connects to the socket - process some data (by sending it to another machine), get the result and send it back to the SAME socket - the data isnt a large value (measured in bytes rather than MB or GB) i TRIED thinking of this in the Asynchronous way - BeginReceive - then pass to the OnClient Connected handler, which calls a Wait For Data
5
4173
by: PH | last post by:
Hi guys; I got a single processor computer, running an application that launches 2 threads. Each of these threads listens for incoming connections in a specific port, so there is a Loop . Until inside each of them.
6
6443
by: roblugt | last post by:
I have what I imagine is a well-known .Net networking problem, but even though I've Googled for some time I've not yet come across a thread where this has been fully explained... There is a fairly common idiom (at least in the Java/UNIX/Win32 worlds) where a socket connection is established and the program then utilises two threads: one dedicated to reading from the socket and the other one concurrently writing to the socket - both using...
2
4537
by: jgbid | last post by:
Hi, I'm trying to build an IP Scanner inc c# for a specific port (80) and for specific IP Ranges. For example 24.36.148.1 to 24.36.148.255 My first step was to use TcpClient, but there are nothing to control Timeout Connection... and I wasn't interested to wait 25-30sec for each ip. After some search, it's look like Socket was the solution with
0
1198
by: Qui Sum | last post by:
I write ip/port scanner so users using search on our LAN know if the particular ftp is online or not. The class I write has the following structure: public class IPScanner { public IPScanner() { } private void connect(object data)
167
8277
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an object should run in its own thread, it should implement this mix-in class. Does this sound like plausible design decision? I'm surprised that C++ doesn't have such functionality, say in its STL. This absence of a thread/object relationship in...
0
2951
by: Jean-Paul Calderone | last post by:
On Sat, 23 Aug 2008 02:25:17 +0800, Leo Jay <python.leojay@gmail.comwrote: No - it's just what I said. create_socket creates one socket and passes it to read_socket and write_socket. read_socket calls connect on the socket it is passed. write_socket calls accept on the socket it is passed. So a single socket has connect and accept called on it. Now, main does call create_socket twice, so this does happen to two sockets, but it's...
0
8306
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7327
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...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2726
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
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
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.