Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Multithreaded and/or nonblocking server?

Reply
Thread Tools

Multithreaded and/or nonblocking server?

 
 
macapone@yahoo.com
Guest
Posts: n/a
 
      02-16-2006
Hi Folks,

I'm trying to write a TCP server that will process and respond to
client XML queries. Nothing new there, plenty of examples on the web.
But here's the catch...

When a client connects, the connection is persistent, similar to a chat
server. the client "chats" a request and gets a response, chats
another request, etc. However, some requests are long-running, and
while waiting for a response to request #1, the client should be able
to send requests #2 and #3 and get responses (without having to open a
new connection to the server).

The only way to do this is to either fork or thread the request
processing code on the server.

Right now I'm using Net:aemon as my server, although I'm certainly
open to alternatives. I've also looked at POE and a few other modules
out there. Basically, I need my handle_request() function to somehow
asynchronously process the request and then communicate the result back
to the client (via event handler? signal? fork? not sure...) when it
has finished processing. It seems as though all the examples out there
are geared towards forking/spawing/etc when a connection is made, and
closing that connection when finished. But I'm looking to keep the
connection open and be able to send requests and get responses
asynchronously.

I'm hoping someone out there has done this and can point me in the
right direction. Any thoughts?

Thanks in advance!
MAC

 
Reply With Quote
 
 
 
 
robic0
Guest
Posts: n/a
 
      02-17-2006
On 16 Feb 2006 15:51:22 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>Hi Folks,
>
>I'm trying to write a TCP server that will process and respond to
>client XML queries. Nothing new there, plenty of examples on the web.
>But here's the catch...
>
>When a client connects, the connection is persistent, similar to a chat
>server. the client "chats" a request and gets a response, chats
>another request, etc. However, some requests are long-running, and
>while waiting for a response to request #1, the client should be able
>to send requests #2 and #3 and get responses (without having to open a
>new connection to the server).
>
>The only way to do this is to either fork or thread the request
>processing code on the server.
>
>Right now I'm using Net:aemon as my server, although I'm certainly
>open to alternatives. I've also looked at POE and a few other modules
>out there. Basically, I need my handle_request() function to somehow
>asynchronously process the request and then communicate the result back
>to the client (via event handler? signal? fork? not sure...) when it
>has finished processing. It seems as though all the examples out there
>are geared towards forking/spawing/etc when a connection is made, and
>closing that connection when finished. But I'm looking to keep the
>connection open and be able to send requests and get responses
>asynchronously.
>
>I'm hoping someone out there has done this and can point me in the
>right direction. Any thoughts?
>
>Thanks in advance!
>MAC


You have said alot there. I havent used Net:aemon (Unix service?).
Are you writing the cline and server?

I don't know about Perl but the server side on a Win32 platform
a simple server model goes something like this:

A single, usually non-threaded routine waits for connect
requests. Upon connect, the user info, ip, etc.. info is recorded into
an array. Then a service thread (the same function every time) is started
to handle just the requests from that client. When the client disconnects,
ahhh, the record of his connect data is expunged.

This happens via many syncronization layers, depending on how complex a
model.

To write the client is trivial.
The general server thread CAN however (if the connect routine is threaded and
queued) host multiple requests from the same ip, etc.. however it complicates
things, but is still trivial.

Doing this in Perl is laughable since it puts the onus on a fixed design dll
w32 layer to be expanded, having to matriculate through the perl interfaces.
Not something likely. Generality at best is what you can expect.

You should learn syncronization control programming, then use w32 (or Unix)
to create your own dll's. After that step, you may shine on Perl altogether.

-good luck-
 
Reply With Quote
 
 
 
 
Eric Schwartz
Guest
Posts: n/a
 
      02-17-2006
robic0 writes:
> Doing this in Perl is laughable since it puts the onus on a fixed design dll
> w32 layer to be expanded, having to matriculate through the perl interfaces.
> Not something likely. Generality at best is what you can expect.


I normally avoid following up on trolls, but this was a thing of
beauty. I have rarely seen such an artful misuse of "matriculate".
Thanks for brightening my day a little.

-=Eric
 
Reply With Quote
 
macapone@yahoo.com
Guest
Posts: n/a
 
      02-17-2006
I should have clarified the platform; it is Unix (Linux), although
Net:aemon does have a perl-win32 port.

My sense is that this is a threading issue or an event issue. Perl is
lamentably remiss in real thread documentation, it being a (relatively)
new addition to the language.

I know that it can be done quite easily in Java (a proof-of-concept
written by a coworker worked well), but tragically, I only speak Perl
for now.

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      02-17-2006
(E-Mail Removed) wrote:
> Hi Folks,
>
> I'm trying to write a TCP server that will process and respond to
> client XML queries. Nothing new there, plenty of examples on the web.
> But here's the catch...


Does the server only talk to one client, or does it have to deal with
multiple concurrent clients as well as multiple concurrent requests per
client?

> When a client connects, the connection is persistent, similar to a chat
> server. the client "chats" a request and gets a response, chats
> another request, etc. However, some requests are long-running, and
> while waiting for a response to request #1, the client should be able
> to send requests #2 and #3 and get responses (without having to open a
> new connection to the server).


Are the responses nice discrete packages? If the response to request #2
is 38 Gig and the server is trying to send that to the client, can the
server refuse to listen to any more requests from the client until the
client has finished reading response #2?

What happens when requests 2, 3, 4, etc. are just as long running as
request 1, and then pretty soon have 1_987_235 pending requests?

>
> The only way to do this is to either fork or thread the request
> processing code on the server.


I'm not sure that that is the only way. I guess the range of options would
depend on the exact nature of the requests. Of course, you also have to
do something just about as challenging on the client, too.


> Right now I'm using Net:aemon as my server, although I'm certainly
> open to alternatives. I've also looked at POE and a few other modules
> out there. Basically, I need my handle_request() function


Net:aemon doesn't have a handle_request method, so it isn't exactly clear
what handle_request() means to you.

> to somehow
> asynchronously process the request and then communicate the result back
> to the client (via event handler? signal? fork? not sure...)


None of those are effective ways of communicating back to a client. Those
are ways of communicating within/among the server.

> when it
> has finished processing. It seems as though all the examples out there
> are geared towards forking/spawing/etc when a connection is made, and
> closing that connection when finished.


You are probably looking in the wrong place. A lot of work in the OS,
TCP/IP, etc, has gone into enabling that strand of copper connecting your
computer to mine to look like an abstraction of a whole bunch of different
connections which we can make and break at will, run in "parallel", etc.
Now it seems like you are trying to take that abstraction, and use Perl to
throw away all of it back down to a virtual strand of copper wire, and then
re-abstract back up again. I don't think many people want to do that in
perl.

Anyway, look into Threads::Running or Thread::Queue. Or just flocking
sockets.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      02-17-2006
(E-Mail Removed) wrote:
> I should have clarified the platform; it is Unix (Linux), although
> Net:aemon does have a perl-win32 port.
>
> My sense is that this is a threading issue or an event issue. Perl is
> lamentably remiss in real thread documentation, it being a (relatively)
> new addition to the language.


What parts of the docs did you read and not understand?

> I know that it can be done quite easily in Java (a proof-of-concept
> written by a coworker worked well), but tragically, I only speak Perl
> for now.


Which specific features in Java did your cow orker use that you couldn't
figure out how to do in Perl?


#server
use strict;
use IO::Socket qw(EFAULT :crlf);
use IO::File;
use Storable;
use threads;
use threads::shared;

my $port = 1426;
my $socket = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $port,
Listen => 1,Reuse => SO_REUSEADDR)
or die "Can't create listen socket: $!";

my $conn;
my $conn_semaphore :shared;

my $x='x'x10_000; # extra data to stress the locking/buffering mechanism

{
$conn = $socket->accept or ($! eq 'Interrupted system call' and redo)
or die $!;
$conn->autoflush(1);
while (my $request=Storable::fd_retrieve($conn)) {
last unless @$request;
threads->create("do_it", $request)->detach;
};
Storable::nstore_fd([],$conn) or die $!; # end of stream marker
close $conn or die $!;
};


sub do_it {
my $request=shift;
#simulate long running queries
select undef,undef,undef,rand();
my $answer = sqrt $request->[1];
{
lock $conn_semaphore;
Storable::nstore_fd([$request->[0],$answer,$x],$conn) or die $!;
};
};

__END__

# client
use strict;
use IO::Socket qw(EFAULT :crlf);
use IO::File;
use Storable;

my $port = 1426;
my $socket = IO::Socket::INET->new( "localhost:$port")
or die "Can't create socket: $!";


##Get ahead of the server
foreach (1..20) {
## Each request is a serial number followed by the query.
Storable::nstore_fd([$_,$_],$socket) or die $!;
};

##But not too much ahead
foreach (21..500) {
Storable::nstore_fd([$_,$_],$socket) or die $!;
my $reply=Storable::fd_retrieve($socket);
print "@$reply[0,1]\n";
};
Storable::nstore_fd([],$socket) or die $!; # end marker

while (1) {
my $reply=Storable::fd_retrieve($socket);
last unless @$reply;
};


Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      02-17-2006
robic0 <> wrote:

> I don't know about Perl



Yes, we can tell.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
DJ Stunks
Guest
Posts: n/a
 
      02-18-2006
Tad McClellan wrote:
> robic0 <> wrote:
>
> > I don't know about Perl

>
>
> Yes, we can tell.


bwah-ha-ha-ha-ha-ha. what a maroon!

nice one, Tad.

-jp

 
Reply With Quote
 
Csaba
Guest
Posts: n/a
 
      02-18-2006
"DJ Stunks" <(E-Mail Removed)> wrote in news:1140226283.891731.266130
@g43g2000cwa.googlegroups.com:

> Tad McClellan wrote:
>> robic0 <> wrote:
>>
>> > I don't know about Perl

>>
>>
>> Yes, we can tell.

>
> bwah-ha-ha-ha-ha-ha. what a maroon!



According to dictionary.com, "maroon" as a noun means

1. often Maroon
1. A fugitive Black slave in the West Indies in the 17th and 18th
centuries.
2. A descendant of such a slave.
2. A person who is marooned, as on an island.


I fail to see the connection, especially since being abandoned on an island
usually means no Usenet access...

--
Life is complex, with real and imaginary parts.
 
Reply With Quote
 
DJ Stunks
Guest
Posts: n/a
 
      02-18-2006

Csaba wrote:
> "DJ Stunks" <(E-Mail Removed)> wrote about robic0:
> >what a maroon!

>
>
> According to dictionary.com, "maroon" as a noun means
>


one cannot always use a dictionary to dissect life's finer points:
http://www.wavsite.com/sounds/57075/bugs10.wav

-jp

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
nonblocking read of one xml element? mh@pixar.com Python 1 02-06-2008 12:38 AM
nonblocking sockets nooneinparticular314159@yahoo.com Java 2 04-08-2006 09:41 AM
strange problem with nonblocking sockets anki C Programming 3 03-01-2005 05:26 AM
nonblocking read() Peter Ammon Python 3 11-17-2004 12:54 AM
setSoTimeout in nonblocking mode? KimTaehwan Java 0 11-16-2003 01:51 AM



Advertisments