473,323 Members | 1,560 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,323 software developers and data experts.

How to stop an [Rpyc] server thread?

I embedded an Rpyc threaded server in a preexistent daemon (an irc
bot), this is actually very simple;
start_threaded_server(port = DEFAULT_PORT)
then I had the necessity to stop the thread which accept() new
connections without killing the whole app, the thread is simply a while
True that spawn a new thread which serves each connection, so I placed
a flag and a break in this way:
def local_threaded_server(port = DEFAULT_PORT, **kw):
global stop
sock = create_listener_socket(port)
while True:
newsock, name = sock.accept()
t = Thread(target = serve_socket, args = (newsock,),
kwargs = kw)
t.setDaemon(True)
t.start()
if stop: break
but, since sock.accept() blocks, when I set stop=True the server wait
one more connection and only then stops.
I tried sock.settimeout(10) before entering the while and so checking
timeout exception on accept() but I experienced a strange behavior, the
clients connections close immediatly, with one of these exceptions on
the client side on their first use of the rpc connection:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Programmi\Python24\lib\site-packages\Rpyc\NetProxy.py", line
82, in __repr__
return self.__request__("handle_repr", *args)
File "C:\Programmi\Python24\lib\site-packages\Rpyc\NetProxy.py", line
113, in __request__
return _get_conn(self).sync_request(handler, _get_oid(self), *args)
File "C:\Programmi\Python24\lib\site-packages\Rpyc\Connection.py",
line 143, in sync_request
self.serve()
File "C:\Programmi\Python24\lib\site-packages\Rpyc\Connection.py",
line 126, in serve
type, seq, data = self.channel.recv()
File "C:\Programmi\Python24\lib\site-packages\Rpyc\Channel.py", line
50, in recv
type, seq, length = struct.unpack(self.HEADER_FORMAT,
self.stream.read(self.HEADER_SIZE))
File "C:\Programmi\Python24\lib\site-packages\Rpyc\Stream.py", line
44, in read
buf = self.sock.recv(count)
socket.error: (10053, 'Software caused connection abort')

OR
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Programmi\Python24\lib\site-packages\Rpyc\NetProxy.py", line
82, in __repr__
return self.__request__("handle_repr", *args)
File "C:\Programmi\Python24\lib\site-packages\Rpyc\NetProxy.py", line
113, in __request__
return _get_conn(self).sync_request(handler, _get_oid(self), *args)
File "C:\Programmi\Python24\lib\site-packages\Rpyc\Connection.py",
line 143, in sync_request
self.serve()
File "C:\Programmi\Python24\lib\site-packages\Rpyc\Connection.py",
line 126, in serve
type, seq, data = self.channel.recv()
File "C:\Programmi\Python24\lib\site-packages\Rpyc\Channel.py", line
50, in recv
type, seq, length = struct.unpack(self.HEADER_FORMAT,
self.stream.read(self.HEADER_SIZE))
File "C:\Programmi\Python24\lib\site-packages\Rpyc\Stream.py", line
46, in read
raise EOFError()
EOFError

I'm not an expert in socket programming, but I can't see the
correlation between the "listener socket" being in timeout mode and a
different behavior the other sockets..
Anyhow the main goal is being able to shut down the thread of the rpyc
server, any other way is an appreciated suggestion.

Sep 7 '06 #1
3 5697

Saizan wrote:
I embedded an Rpyc threaded server in a preexistent daemon (an irc
bot), this is actually very simple;
start_threaded_server(port = DEFAULT_PORT)
then I had the necessity to stop the thread which accept() new
connections without killing the whole app, the thread is simply a while
True that spawn a new thread which serves each connection, so I placed
a flag and a break in this way:
def local_threaded_server(port = DEFAULT_PORT, **kw):
global stop
sock = create_listener_socket(port)
while True:
newsock, name = sock.accept()
t = Thread(target = serve_socket, args = (newsock,),
kwargs = kw)
t.setDaemon(True)
t.start()
if stop: break
First off, instead of the "while True" and the break, you could write:
while not stop:
but, since sock.accept() blocks, when I set stop=True the server wait
one more connection and only then stops.
I tried sock.settimeout(10) before entering the while and so checking
timeout exception on accept() but I experienced a strange behavior, the
clients connections close immediatly, with one of these exceptions on
the client side on their first use of the rpc connection:
[snip]
I'm not an expert in socket programming, but I can't see the
correlation between the "listener socket" being in timeout mode and a
different behavior the other sockets..
Anyhow the main goal is being able to shut down the thread of the rpyc
server, any other way is an appreciated suggestion.
Now to the real issue. I've also had such weird problems with socket
timeout in Python. The best workaround I found is to use select() to
check for activity on the socket(s), and use select()'s timeout
mechanism. So far, this has worked without a hitch on both WindowsXP
and Solaris Sparc9 installations.

- Tal
reduce(lambda m,x:[m[i]+s[-1] for i,s in enumerate(sorted(m))],
[[chr(154-ord(c)) for c in '.&-&,l.Z95193+179-']]*18)[3]

Sep 8 '06 #2
Saizan wrote:
[...]
I tried sock.settimeout(10) before entering the while and so checking
timeout exception on accept() but I experienced a strange behavior, the
clients connections close immediatly, with one of these exceptions on
the client side on their first use of the rpc connection:
[...]
socket.error: (10053, 'Software caused connection abort')
[...]
EOFError

I'm not an expert in socket programming, but I can't see the
correlation between the "listener socket" being in timeout mode and a
different behavior the other sockets..
After modest investigation, it looks like a bug in Python's
sockets, at least on WinXP.

Try inserting the line after accept():

[...]
newsock, name = sock.accept()
newsock.settimeout(None)
[...]
--
--Bryan
Sep 12 '06 #3
7 Sep 2006 23:38:08 -0700, Tal Einat <ta************@gmail.com>:
I'm not an expert in socket programming, but I can't see the
correlation between the "listener socket" being in timeout mode and a
different behavior the other sockets..
Anyhow the main goal is being able to shut down the thread of the rpyc
server, any other way is an appreciated suggestion.

Now to the real issue. I've also had such weird problems with socket
timeout in Python. The best workaround I found is to use select() to
check for activity on the socket(s), and use select()'s timeout
mechanism. So far, this has worked without a hitch on both WindowsXP
and Solaris Sparc9 installations.
Twisted[1] is the answer. I've never seen a better framework for using
sockets, it's painless. I created two versions of the same protocol
(both client and server), one using sockets + select, another using
Twisted. The sockets version had 2x lines than the Twisted one and
lots of bugs. Sockets may fail *anywhere* in your code, and Twisted
takes care of all details for you[2]. Simply Sweet.

Cheers,

[1] http://www.twistedmatrix.com/
[2] Of couse this is just *one* advantage of the Twisted framework...

PS.: No, I wasn't paid for this e-mail ;-)

--
Felipe.
Sep 12 '06 #4

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

Similar topics

1
by: magic man via .NET 247 | last post by:
hi everyone i have a c# application that uses multithreading toconnect to sql server and execute a stored procedure on theserver. i am using a dataset,sqlcommand,dataadapter and adatagrid to carry...
1
by: gangesmaster | last post by:
the RPyC's project page has moved to http://rpyc.wikispaces.com the old site (http://rpyc.sourceforge.net) redirects there now. because it's the official site, i chose to limit changes to...
0
by: gangesmaster | last post by:
Remote Python Call 2.50 release-candidate http://rpyc.wikispaces.com -tomer
0
by: gangesmaster | last post by:
Remote Python Call (RPyC) - transparent and symmetrical python RPC and distributed computing library download and info: http://rpyc.wikispaces.com full changelog:...
0
by: gangesmaster | last post by:
Remote Python Call (RPyC) has been released. this release introduces delivering objects, reducing memory consumption with __slots__, and several other new/improved helper functions. see the release...
9
by: semedao | last post by:
Hi, I am using sync and async operations on the same socket. generally I want the socket to wait on BeginReceive and to not block the object thread. but in some cases I want to stop the...
7
by: Marc Bartsch | last post by:
Hi, I have a background worker in my C# app that makes a synchronous HttpWebRequest.GetResponse() call. The idea is to POST a file to a server on the internet. When I call HttpWebRequest.Abort()...
0
by: Blubaugh, David A. | last post by:
To All, Has any one out there ever worked with the Rpyc, which is a remote process call for python? David Blubaugh
1
by: Jeffrey Barish | last post by:
skip@pobox.com wrote: So I thought at first, but then I saw this statement in the documentation: It is possible to run a manager server on one machine and have clients use it from other...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.