Commit 9b271698 authored by Likun Zhang's avatar Likun Zhang
Browse files

Fix the revision number in Changelog. Remove the folder...

Fix the revision number in Changelog. Remove the folder /src/lib/python/isc/utils(which was forgot to removed in r3382. ), trivial, skip review.

git-svn-id: svn://bind10.isc.org/svn/bind10/trunk@3407 e5f2f494-b856-4b98-b285-d166d9295462
parent 99f0a885
......@@ -7,7 +7,7 @@
renamed to 'util'. Programs that used 'import isc.utils.process'
now need to use 'import isc.util.process'. The folder
/src/lib/python/isc/Util is removed since it isn't used by any
program. (Trac #364, rTBD)
program. (Trac #364, r3382)
112. [func] zhang likun
Add one mixin class to override the naive serve_forever() provided
......
SUBDIRS = tests
python_PYTHON = __init__.py process.py socketserver_mixin.py
pythondir = $(pyexecdir)/isc/utils
from isc.utils.socketserver_mixin import *
# Copyright (C) 2010 CZ NIC
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""
Module to manipulate the python processes.
It contains only function to rename the process, which is currently
wrapper around setproctitle library. Does not fail if the setproctitle
module is missing, but does nothing in that case.
"""
try:
from setproctitle import setproctitle
except ImportError:
def setproctitle(_): pass
import sys
import os.path
"""
Rename the current process to given name (so it can be found in ps).
If name is None, use zero'th command line argument.
"""
def rename(name=None):
if name is None:
name = os.path.basename(sys.argv[0])
setproctitle(name)
# Copyright (C) 2010 Internet Systems Consortium.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import threading
import socket
import select
SOCK_DATA = b'somedata'
class NoPollMixIn:
'''This is a mix-in class to override the function serve_forever()
and shutdown() in class socketserver.BaseServer.
As commented in the module source code, serve_forever() in
socketserver.BaseServer uses polling for a shutdown request, which
"reduces the responsiveness to a shutdown request and wastes cpu at
all other times."
This class fixes this problem by introducing internal message
passing via a separate socket. Note, however, that according to
the module documentation serve_forever() and shutdown() are not
categorized as functions that can be overridden via mix-ins. So
this mix-in class may not be compatible with future versions of
socketserver. It should be considered a short term workaround
until the original implementation is fixed.
The NoPollMixIn class should be used together with
socketserver.BaseServer or some derived classes of it, and it must
be placed before the corresponding socketserver class. In
addition, the constructor of this mix-in must be called
explicitely in the derived class. For example, a basic TCP server
without the problem of polling is created as follows:
class MyServer(NoPollMixIn, socketserver.TCPServer):
def __init__(...):
...
NoPollMixIn.__init__(self)
...
To shutdown the server correctly, the serve_forever() method must
be run in a separate thread, and shutdown() must be called from
some other thread.
'''
def __init__(self):
self.__read_sock, self.__write_sock = socket.socketpair()
self._is_shut_down = threading.Event()
def serve_forever(self, poll_interval=None):
''' Overrides the serve_forever([poll_interval]) in class
socketserver.BaseServer.
It uses a socketpair to wake up the select when shutdown() is
called in anther thread. Note, parameter 'poll_interval' is
just used for interface compatibility; it's never used in this
function.
'''
while True:
# block until the self.socket or self.__read_sock is readable
try:
r, w, e = select.select([self, self.__read_sock], [], [])
except select.error as err:
if err.args[0] == EINTR:
continue
else:
break
if self.__read_sock in r:
break
else:
self._handle_request_noblock()
self._is_shut_down.set()
def shutdown(self):
'''Stops the serve_forever loop.
Blocks until the loop has finished, the function should be called
in another thread when serve_forever is running, or it will block.
'''
self.__write_sock.send(SOCK_DATA) # make self.__read_sock readable.
self._is_shut_down.wait() # wait until the serve thread terminate
PYTESTS = process_test.py socketserver_mixin_test.py
EXTRA_DIST = $(PYTESTS)
# later will have configure option to choose this, like: coverage run --branch
PYCOVERAGE = $(PYTHON)
# test using command-line arguments, so use check-local target instead of TESTS
check-local:
for pytest in $(PYTESTS) ; do \
echo Running test: $$pytest ; \
env PYTHONPATH=$(abs_top_srcdir)/src/lib/python:$(abs_top_builddir)/src/lib/python:$(abs_top_builddir)/src/lib/dns/python/.libs \
$(PYCOVERAGE) $(abs_srcdir)/$$pytest || exit ; \
done
# Copyright (C) 2010 CZ NIC
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""Tests for isc.utils.process."""
import unittest
import isc.utils.process
run_tests = True
try:
import setproctitle
except ImportError:
run_tests = False
class TestRename(unittest.TestCase):
"""Testcase for isc.process.rename."""
def __get_self_name(self):
return setproctitle.getproctitle()
@unittest.skipIf(not run_tests, "Setproctitle not installed, not testing")
def test_rename(self):
"""Test if the renaming function works."""
isc.utils.process.rename("rename-test")
self.assertEqual("rename-test", self.__get_self_name())
isc.utils.process.rename()
self.assertEqual("process_test.py", self.__get_self_name())
if __name__ == "__main__":
unittest.main()
# Copyright (C) 2010 Internet Systems Consortium.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import unittest
from isc.utils.socketserver_mixin import NoPollMixIn
import socketserver
import threading
import socket
import time
class MyHandler(socketserver.BaseRequestHandler):
def handle(self):
data = self.request.recv(20)
self.request.send(data)
class MyServer(NoPollMixIn,
socketserver.ThreadingMixIn,
socketserver.TCPServer):
def __init__(self, server_addr, handler_class):
NoPollMixIn.__init__(self)
socketserver.TCPServer.__init__(self, server_addr, handler_class)
def send_and_get_reply(ip, port, msg):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
sock.send(msg)
response = sock.recv(20)
sock.close()
return response
class TestNoPollMixIn(unittest.TestCase):
def test_serve_forever(self):
# use port 0 to select an arbitrary unused port.
server = MyServer(('127.0.0.1', 0), MyHandler)
ip, port = server.server_address
server_thread = threading.Thread(target=server.serve_forever)
server_thread.start()
msg = b'senddata'
self.assertEqual(msg, send_and_get_reply(ip, port, msg))
self.assertTrue(server_thread.is_alive())
self.assertFalse(server._is_shut_down.is_set())
server.shutdown() # Now shutdown the server
self.assertTrue(server._is_shut_down.is_set())
if __name__== "__main__":
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment