473,581 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Tru e)
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\P ython24\lib\sit e-packages\Rpyc\N etProxy.py", line
82, in __repr__
return self.__request_ _("handle_repr" , *args)
File "C:\Programmi\P ython24\lib\sit e-packages\Rpyc\N etProxy.py", line
113, in __request__
return _get_conn(self) .sync_request(h andler, _get_oid(self), *args)
File "C:\Programmi\P ython24\lib\sit e-packages\Rpyc\C onnection.py",
line 143, in sync_request
self.serve()
File "C:\Programmi\P ython24\lib\sit e-packages\Rpyc\C onnection.py",
line 126, in serve
type, seq, data = self.channel.re cv()
File "C:\Programmi\P ython24\lib\sit e-packages\Rpyc\C hannel.py", line
50, in recv
type, seq, length = struct.unpack(s elf.HEADER_FORM AT,
self.stream.rea d(self.HEADER_S IZE))
File "C:\Programmi\P ython24\lib\sit e-packages\Rpyc\S tream.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\P ython24\lib\sit e-packages\Rpyc\N etProxy.py", line
82, in __repr__
return self.__request_ _("handle_repr" , *args)
File "C:\Programmi\P ython24\lib\sit e-packages\Rpyc\N etProxy.py", line
113, in __request__
return _get_conn(self) .sync_request(h andler, _get_oid(self), *args)
File "C:\Programmi\P ython24\lib\sit e-packages\Rpyc\C onnection.py",
line 143, in sync_request
self.serve()
File "C:\Programmi\P ython24\lib\sit e-packages\Rpyc\C onnection.py",
line 126, in serve
type, seq, data = self.channel.re cv()
File "C:\Programmi\P ython24\lib\sit e-packages\Rpyc\C hannel.py", line
50, in recv
type, seq, length = struct.unpack(s elf.HEADER_FORM AT,
self.stream.rea d(self.HEADER_S IZE))
File "C:\Programmi\P ython24\lib\sit e-packages\Rpyc\S tream.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 5738

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(Tru e)
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(sorte d(m))],
[[chr(154-ord(c)) for c in '.&-&,l.Z95193+1 79-']]*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.settime out(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
2236
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 out the process on a background thread andeverything goes well but the problem arrises when i created astop button that attempts to cancel the...
1
1446
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 members only. it's so much easier to maintain the wiki that the crappy htmls at sourceforge :) anyway, the new site contains much more info and is...
0
909
by: gangesmaster | last post by:
Remote Python Call 2.50 release-candidate http://rpyc.wikispaces.com -tomer
0
1031
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: http://rpyc.wikispaces.com/changelog release notes: http://rpyc.wikispaces.com/release+notes major changes: * added isinstance and issubclass for remote objects * moved to...
0
957
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 notes and changelog (on the site) for more info. home: http://rpyc.wikispaces.com -tomer
9
7315
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 BeginReceive in the middle - Don't accept any data from it , and using regular Receive (I don't want the data will come to the BeginReceive byte buffer ,...
7
7668
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() on the request object on another thread, GetResponse() returns with an exception as expected. However, when I monitor the network traffic, it does...
0
936
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
4415
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 machines (assuming that the firewalls involved allow it). -- Jeffrey Barish
0
7862
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...
0
7789
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...
0
8301
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8169
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...
1
5670
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...
0
3803
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...
0
3820
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2300
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
0
1132
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...

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.