473,405 Members | 2,404 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,405 software developers and data experts.

Writing Multithreaded Client-Server in Python.

Hi,
I tried finding and example of multithreaded client-serve program in
python. Can any one please tell me how to write a multithreaded
client-server programn in python such that
1.It can handle multiple connections
2.It uses actual threads and not select() or some other function

Aug 30 '05 #1
7 5292
"Sidd" <ia*****@gmail.com> writes:
I tried finding and example of multithreaded client-serve program in
python. Can any one please tell me how to write a multithreaded
client-server programn in python such that
1.It can handle multiple connections
2.It uses actual threads and not select() or some other function


If you mean multiple threads on the server side, see the SocketServer
module and its ThreadingMixin class. You may have to look at the
source code since up until recently the docs were incomplete. There
is a big helpful comment at the top of SocketServer.py which recently
got merged into the library reference manual.
Aug 30 '05 #2

Paul Rubin schreef:
"Sidd" <ia*****@gmail.com> writes:
I tried finding and example of multithreaded client-serve program in
python. Can any one please tell me how to write a multithreaded
client-server programn in python such that
1.It can handle multiple connections
2.It uses actual threads and not select() or some other function


If you mean multiple threads on the server side, see the SocketServer
module and its ThreadingMixin class. You may have to look at the
source code since up until recently the docs were incomplete. There
is a big helpful comment at the top of SocketServer.py which recently
got merged into the library reference manual.


Yes, however the term 'ThreadingMixIn' is a bit confusing (at least it
was to me).
What it does do, is handle each request (from the same client too) in a
new separate thread. Convenient if your processing intensive handle may
otherwise slow down the main server process becoming less responsive to
other requests.
What it doesn't do (and what Sidd seems to search as is suggested by
his 'select()' remark) is handle each client in a separate thread.

This is in fact listed in the (scarce) doc as a 'ToDo' of the
SocketServer if my memory serves me right.
If you want to apply SocketServer such that each client corresponds to
one thread that handles its' requests (and maintains its state), don't
use ThreadingMixIn - only the thread-selection will be executed in a
separate thread.
You could maintain a dictionary of Threads running RequestHandlers for
each client in the server class.
I use a new Handler for each incoming request, parse a username
parameter from the message and select the thread-handler from the
server dict. Each handler in the dict contains an open outgoing socket,
so I can also e.g. broadcast and notify clients.
Alternatively, you can (probably) keep the client2server socket open as
well (on both client and server). Thread selection should then
(hopefully :-) happen magically by calling the handle() method in each
thread directly.

HTH
Thijs

Aug 30 '05 #3
go****@phaedro.com writes:
What it does do, is handle each request (from the same client too) in a
new separate thread. Convenient if your processing intensive handle may
otherwise slow down the main server process becoming less responsive to
other requests.
What it doesn't do (and what Sidd seems to search as is suggested by
his 'select()' remark) is handle each client in a separate thread.
I don't know what you mean by that. It launches a new thread for each
client connection. The connection is two-way and can last as long as
desired. If you're imagining something like a web server handling
http requests, using http 1.1 keepalive, you could handle any number
of requests in that connection.
If you want to apply SocketServer such that each client corresponds to
one thread that handles its' requests (and maintains its state), don't
use ThreadingMixIn - only the thread-selection will be executed in a
separate thread.


What do you mean by "each client"? If a client connects, does some
stuff, disconnects, and later reconnects, how do you know that it's
the same client that's come back?
Aug 30 '05 #4
Paul Rubin wrote:
go****@phaedro.com writes:
What it does do, is handle each request (from the same client too) in a
new separate thread. Convenient if your processing intensive handle may
otherwise slow down the main server process becoming less responsive to
other requests.
What it doesn't do (and what Sidd seems to search as is suggested by
his 'select()' remark) is handle each client in a separate thread.

I don't know what you mean by that. It launches a new thread for each
client connection. The connection is two-way and can last as long as
desired. If you're imagining something like a web server handling
http requests, using http 1.1 keepalive, you could handle any number
of requests in that connection.

I suspect he was trying to say that BaseHTTPServer has no mechanism for
handling state. As you know, of course, this is most relevant across
multiple successive connections to a server from the same client, and
has little to do with threads.
If you want to apply SocketServer such that each client corresponds to
one thread that handles its' requests (and maintains its state), don't
use ThreadingMixIn - only the thread-selection will be executed in a
separate thread.

What do you mean by "each client"? If a client connects, does some
stuff, disconnects, and later reconnects, how do you know that it's
the same client that's come back?


The assertion that ThreadingMixIn doesn't handle *sessions* might be
more appropriate, but then there's no reason why it really should (since
if they were handled at all they would be better handled in the base
server classes). By "each client" I suspect the OP really meant "each
session", and was ignoring the fact that the same client can have
multiple sessions to the same server.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Aug 30 '05 #5

Steve Holden schreef:
Paul Rubin wrote:
go****@phaedro.com writes:
What it doesn't do (and what Sidd seems to search as is suggested by
his 'select()' remark) is handle each client in a separate thread.

I don't know what you mean by that. It launches a new thread for each
client connection. The connection is two-way and can last as long as
desired. If you're imagining something like a web server handling
http requests, using http 1.1 keepalive, you could handle any number
of requests in that connection.

I suspect he was trying to say that BaseHTTPServer has no mechanism for
handling state. As you know, of course, this is most relevant across
multiple successive connections to a server from the same client, and
has little to do with threads.


see below: I must apologize for not being able to use the standard CS
vernacular but indeed I meant session (persistent state over multiple
requests) - I still think that the thread-starter looks for a mechanism
that handles each *session* in a thread where the 'mother'
server-thread selects the thread belonging to a session.
If you want to apply SocketServer such that each client corresponds to
one thread that handles its' requests (and maintains its state), don't
use ThreadingMixIn - only the thread-selection will be executed in a
separate thread.

What do you mean by "each client"? If a client connects, does some
stuff, disconnects, and later reconnects, how do you know that it's
the same client that's come back?


The assertion that ThreadingMixIn doesn't handle *sessions* might be
more appropriate, but then there's no reason why it really should (since
if they were handled at all they would be better handled in the base
server classes). By "each client" I suspect the OP really meant "each
session", and was ignoring the fact that the same client can have
multiple sessions to the same server.


Correct. My own 'brew' is multi-session-clients enabled (in fact I test
with two applets from the same PC) but indeed I used confusing language
saying 'client' for 'session'.

Again: ThreadingMixIn doesn't give you 'session threads' in which you
store persistent information - a candidate for some generic extension
of SocketServer ?

When doing research for my own hobby project, I stumbled upon "Twisted"
, it seems to give a lot in terms client-server features/functionality
compared to SocketServer ? Is it indeed a 'generic network programming
framework'? Anyone has experience with it ?
Yours,

--
Thijs - phaedro.com

Sep 1 '05 #6
go****@phaedro.com writes:
I suspect he was trying to say that BaseHTTPServer has no mechanism for
handling state. As you know, of course, this is most relevant across
multiple successive connections to a server from the same client, and
has little to do with threads.

Usually you would do that with browser cookies.
Correct. My own 'brew' is multi-session-clients enabled (in fact I test
with two applets from the same PC) but indeed I used confusing language
saying 'client' for 'session'.

Again: ThreadingMixIn doesn't give you 'session threads' in which you
store persistent information - a candidate for some generic extension
of SocketServer ?


What exactly do you mean by session? Let's say the client does
the following:

1. Sends
GET /abcde HTTP/1.1
Connection: Keep-Alive
and downloads the abcde page

2. Sends
GET /fghij HTTP/1.1
and downloads the fghij page, re-using the TCP connection from step 1

3. Opens a completely new TCP connection and sends
GET /klmno HTTP/1.1
through the new connection, and downloads klmno through that
connection.

How many sessions took place here? It sounds like you want the answer
to be one session, that operations 2 and 3 are part of the same session.
But that makes no sense unless you associate them somehow, say using
a client cookie and a global session table indexed by the cookie.

Why don't you take a look at any of the web frameworks that get
discussed here (Spyce, CherryPy, Zope, Django, whatever). They all
have mechanisms like that. BaseHTTPServer doesn't try to operate
at that level.
Sep 1 '05 #7
a bit of a late reply, sorry...

Paul Rubin schreef:
go****@phaedro.com writes:
I suspect he was trying to say that BaseHTTPServer has no mechanism for
handling state. As you know, of course, this is most relevant across
multiple successive connections to a server from the same client, and
has little to do with threads.
Usually you would do that with browser cookies.

Mwah... Not if you want the client to be responsive to server-initiated
messages, i.e. to implement some sort of listener ? (Or you should use
Jason-RPC or some other relatively exotic tech?)
And, and my guess is this is more applicable in the context of OP's
question, not if you want to prevent constructing some
client-representing object from a cookie-id for each incoming request
(istd. of forwarding the request to a thread holding the
client-representing object in the correct current state)
Correct. My own 'brew' is multi-session-clients enabled (in fact I test
with two applets from the same PC) but indeed I used confusing language
saying 'client' for 'session'.

Again: ThreadingMixIn doesn't give you 'session threads' in which you
store persistent information - a candidate for some generic extension
of SocketServer ?


What exactly do you mean by session?


<snap HTTP session trace>

I mean (in a C/S context) : "A persistent state initiated by a client
(e.g. a login- or first-message event) and maintained by the server."

How many sessions took place here?
Though trivial: 2 'TCP sessions' but I am (sorry for the confusion) not
talking about TCP sessions but (and I guess OP too) application-level
sessions.
It sounds like you want the answer
to be one session, that operations 2 and 3 are part of the same session.
But that makes no sense unless you associate them somehow, say using
a client cookie and a global session table indexed by the cookie.

In my world, message 3 would be 'session initiating'.
Message 2 could be handled on the basis of a possibly changed 'client
state' due to processing of message 1 (or other intermediate message
exchange).
Why don't you take a look at any of the web frameworks that get
discussed here (Spyce, CherryPy, Zope, Django, whatever). They all
have mechanisms like that. BaseHTTPServer doesn't try to operate
at that level.


The OP had a question about SocketServer not BaseHTTPServer? That
certainly doesn't give you 'persistent state mechanism' for free.
SocketServer doesn't either - all I wanted to say is I got myself
confused a bit by the name "ThreadingMixIn" suggesting to me that it
would give me a thread to store state info in - again it doesn't .

I know Zope very well, and CherryPy a bit. At least for my project they
wouldn't suffice because, as far as I know, they don't offer a
mechanism for the server to 'raise an event' or 'send a non-response
message' to the client. I don't know about Djange and Spyce but thanks:
I'll certainly study them ! I suggested Twisted as an 'out-of-the-box'
framework for state-persistent, real-time C/S framework but I perceived
the overhead/learning-curve intimidating and decided to pass.
--
T.
www.phaedro.com
Compile-time type-checking is a drag.
http://www.bushclintonkatrinafund.com/ - help now but never ever vote
republican again please

Sep 6 '05 #8

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

Similar topics

2
by: pradyumna | last post by:
In Project settins - C/C++ - Code Generation, what is the difference between the option "Multithreaded" and "Multithreaded DLL". I understand that on selecting multithreaded option, single and...
6
by: Frank Rizzo | last post by:
Hello, I am writing a server that will be inserting/reading/updating the database on multiple threads. I understand that you can't have more than 1 reader open on a database connection. Are...
7
by: Pavils Jurjans | last post by:
Hello, I wanted to get some light in the subject. As I develop ASP.NET applications, it's necessary to understand how exactly the server- communication happens. It seems like initially...
5
by: Mark Harrison | last post by:
I'm looking for feedback from anybody who has used pg in a multi-threaded program, particularly one in which several threads each open a database connection. It's documented to work in that...
3
by: groups | last post by:
Hi all, I've recently ported a rather large C application to run multithreaded. A few functions have seriously deteriorated in performance, in particular when accessing a rather large global...
9
by: bonk | last post by:
Does anyone have a simple example on how to prohibit that any thread other than the current thread modifies a certain object (a collection) while we are in a certain section of the code? In other...
3
by: | last post by:
Is it possible to have just a multithreaded sub procedure? What I need is a timer time_elapsed event (2 sec interval) send params to a sub that is multithreaded. I have a COM component used to...
3
by: Jake K | last post by:
I have a multithreaded application that I now want to convert into a Windows Service. Does application.run work in a windows service? Are there things to take into consideration when creating a...
3
by: Ronen Yacov | last post by:
Hi everybody, When declaring a singleton, we write: static CMySingle::instance() { static CMySingle instance; return instance; }
7
by: gnanapoongothai | last post by:
hi, the error i am getting in main for allocating heap memory for threads is #include <stdio.h> #include "winsock2.h" #include <stdlib.h> #include <windows.h> #include <strsafe.h>...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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,...
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
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...
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,...
0
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...

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.