[Python-checkins] r88012 - python/branches/py3k/Tools/ccbench/ccbench.py

antoine.pitrou python-checkins at python.org
Sat Jan 15 12:39:23 CET 2011


Author: antoine.pitrou
Date: Sat Jan 15 12:39:23 2011
New Revision: 88012

Log:
Avoid ResourceWarnings in ccbench



Modified:
   python/branches/py3k/Tools/ccbench/ccbench.py

Modified: python/branches/py3k/Tools/ccbench/ccbench.py
==============================================================================
--- python/branches/py3k/Tools/ccbench/ccbench.py	(original)
+++ python/branches/py3k/Tools/ccbench/ccbench.py	Sat Jan 15 12:39:23 2011
@@ -276,19 +276,19 @@
     return sock.recv(n).decode('ascii')
 
 def latency_client(addr, nb_pings, interval):
-    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
-    _time = time.time
-    _sleep = time.sleep
-    def _ping():
-        _sendto(sock, "%r\n" % _time(), addr)
-    # The first ping signals the parent process that we are ready.
-    _ping()
-    # We give the parent a bit of time to notice.
-    _sleep(1.0)
-    for i in range(nb_pings):
-        _sleep(interval)
+    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
+        _time = time.time
+        _sleep = time.sleep
+        def _ping():
+            _sendto(sock, "%r\n" % _time(), addr)
+        # The first ping signals the parent process that we are ready.
         _ping()
-    _sendto(sock, LAT_END + "\n", addr)
+        # We give the parent a bit of time to notice.
+        _sleep(1.0)
+        for i in range(nb_pings):
+            _sleep(interval)
+            _ping()
+        _sendto(sock, LAT_END + "\n", addr)
 
 def run_latency_client(**kwargs):
     cmd_line = [sys.executable, '-E', os.path.abspath(__file__)]
@@ -363,6 +363,7 @@
     for t in threads:
         t.join()
     process.wait()
+    sock.close()
 
     for recv_time, chunk in chunks:
         # NOTE: it is assumed that a line sent by a client wasn't received


More information about the Python-checkins mailing list