From b7e50e0b3a773d0eb57ee1b109d1358631405f4e Mon Sep 17 00:00:00 2001 From: sunshine Date: Fri, 6 Aug 2004 13:36:41 +0000 Subject: [PATCH] Replaced time.clock() -> time.time() (Bug on Unix) --- apps/sam/python/src/i2p/samclasses.py | 23 +++++++++++-------- apps/sam/python/src/i2p/select.py | 4 ++-- .../python/src/i2p/test/test_samclasses.py | 12 +++++----- apps/sam/python/src/i2p/test/test_socket.py | 12 +++++----- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/apps/sam/python/src/i2p/samclasses.py b/apps/sam/python/src/i2p/samclasses.py index f6e043b08..91399fd2a 100644 --- a/apps/sam/python/src/i2p/samclasses.py +++ b/apps/sam/python/src/i2p/samclasses.py @@ -53,6 +53,9 @@ from i2p.pylib import socket as pysocket # Import Python socket # --------------------------------------------------------- def sleep(): time.sleep(0.01) # Sleep between thread polls +def time(): return time.time() # High resolution timer + # Do NOT use time.clock() as it + # drops sleep() time on Linux. log = False # Logging flag. Logs to ./log.txt. @@ -402,11 +405,11 @@ class StreamSession(BaseSession): ' DESTINATION=' + str(dest)) # Now wait until the stream's .didconnect flag is set to True. - if timeout != None: end = time.clock() + timeout + if timeout != None: end = time() + timeout while True: self.term.check() if ans.didconnect: break - if timeout != None and time.clock() >= end: break + if timeout != None and time() >= end: break sleep() return ans @@ -429,7 +432,7 @@ class StreamSession(BaseSession): if self.max_accept <= 0: raise i2p.Error('listen(n) must be called before accept ' + '(n>=1)') - if timeout != None: end = time.clock() + timeout + if timeout != None: end = time() + timeout while True: self.term.check() # Synchronized @@ -439,7 +442,7 @@ class StreamSession(BaseSession): if self.qaccept.qsize() > 0: return self.term.queue_get(self.qaccept) finally: self.lock.release() - if timeout != None and time.clock() >= end: break + if timeout != None and time() >= end: break sleep() # Handle timeout and blocking errors @@ -565,7 +568,7 @@ class Stream: minlen = 1 if waitall: minlen = n - if timeout != None: end = time.clock() + timeout + if timeout != None: end = time() + timeout while True: # Synchronized check and read until data available. self.parent.term.check() @@ -580,7 +583,7 @@ class Stream: # Ungraceful close: raise an error. if self.err != None: raise self.err finally: self.lock.release() - if timeout != None and time.clock() >= end: break + if timeout != None and time() >= end: break sleep() # Handle timeout and blocking error @@ -669,7 +672,7 @@ class DatagramSession(BaseSession): forever). If still no packet is available, raises BlockError or Timeout. Returns the pair (data, address). If peek is True, the data is not removed.""" - if timeout != None: end = time.clock() + timeout + if timeout != None: end = time() + timeout while True: self.term.check() # Synchronized check and read until data available. @@ -683,7 +686,7 @@ class DatagramSession(BaseSession): else: return self.buf.pop_first() finally: self.lock.release() - if timeout != None and time.clock() >= end: break + if timeout != None and time() >= end: break sleep() # Handle timeout and blocking error @@ -737,7 +740,7 @@ class RawSession(BaseSession): def recv(self, timeout=None, peek=False): """Identical to DatagramSocket.recv. The from address is an empty string.""" - if timeout != None: end = time.clock() + timeout + if timeout != None: end = time() + timeout while True: self.term.check() # Synchronized check and read until data available. @@ -751,7 +754,7 @@ class RawSession(BaseSession): else: return self.buf.pop_first() finally: self.lock.release() - if timeout != None and time.clock() >= end: break + if timeout != None and time() >= end: break sleep() # Handle timeout and blocking error diff --git a/apps/sam/python/src/i2p/select.py b/apps/sam/python/src/i2p/select.py index a2aa94cbc..86c932c57 100644 --- a/apps/sam/python/src/i2p/select.py +++ b/apps/sam/python/src/i2p/select.py @@ -144,13 +144,13 @@ def select(readlist, writelist, errlist, timeout=None): sockets. See select.select() in the Python library for more information.""" - if timeout != None: end = time.clock() + timeout + if timeout != None: end = time.time() + timeout while True: # FIXME: Check performance. # Use pyselect.poll for Python sockets, if needed for speed. (Rans, Wans, Eans) = _noblock_select(readlist,writelist,errlist) - if timeout != None and time.clock() >= end: break + if timeout != None and time.time() >= end: break if len(Rans) != 0 or len(Wans) != 0 or len(Eans) != 0: # One or more sockets are ready. if timeout != 0.0: diff --git a/apps/sam/python/src/i2p/test/test_samclasses.py b/apps/sam/python/src/i2p/test/test_samclasses.py index fb4245aee..95f53fb31 100644 --- a/apps/sam/python/src/i2p/test/test_samclasses.py +++ b/apps/sam/python/src/i2p/test/test_samclasses.py @@ -240,8 +240,8 @@ def multithread_packet_test(raw=True): while __done_count < n: time.sleep(0.01) # Read any left-over received packets. - end_time = time.clock() + multithread_wait_time - while time.clock() < end_time: + end_time = time.time() + multithread_wait_time + while time.time() < end_time: # Read any available packets. try: (p, fromaddr) = C.recv(timeout=0.0) except socket.BlockError: p = None @@ -257,7 +257,7 @@ def multithread_packet_test(raw=True): if len(C_got) == len(C_recv) and len(D_got) == len(D_recv): break - if time.clock() >= end_time: + if time.time() >= end_time: may_need_increase = True C_got.sort() @@ -367,8 +367,8 @@ def multithread_stream_test(): while __done_count < n: time.sleep(0.01) # Read any left-over received string data. - end_time = time.clock() + multithread_wait_time - while time.clock() < end_time: + end_time = time.time() + multithread_wait_time + while time.time() < end_time: # Read any available string data, non-blocking. try: p = Cin.recv(100000, timeout=0.0) except socket.BlockError: p = None @@ -382,7 +382,7 @@ def multithread_stream_test(): len(''.join(D_got)) == len(''.join(D_recv)): break - if time.clock() >= end_time: + if time.time() >= end_time: may_need_increase = True C_got = ''.join(C_got) diff --git a/apps/sam/python/src/i2p/test/test_socket.py b/apps/sam/python/src/i2p/test/test_socket.py index 987e5ec21..916330433 100644 --- a/apps/sam/python/src/i2p/test/test_socket.py +++ b/apps/sam/python/src/i2p/test/test_socket.py @@ -132,8 +132,8 @@ def packet_test(raw=True): while __done_count < n: time.sleep(0.01) # Read any left-over received packets. - end_time = time.clock() + multithread_wait_time - while time.clock() < end_time: + end_time = time.time() + multithread_wait_time + while time.time() < end_time: # Read any available packets. try: (p, fromaddr) = C.recvfrom(1000, socket.MSG_DONTWAIT) except socket.BlockError: p = None @@ -149,7 +149,7 @@ def packet_test(raw=True): if len(C_got) == len(C_recv) and len(D_got) == len(D_recv): break - if time.clock() >= end_time: + if time.time() >= end_time: may_need_increase = True C_got.sort() @@ -263,8 +263,8 @@ def stream_test(): while __done_count < n: time.sleep(0.01) # Read any left-over received string data. - end_time = time.clock() + multithread_wait_time - while time.clock() < end_time: + end_time = time.time() + multithread_wait_time + while time.time() < end_time: # Read any available string data, non-blocking. try: p = Cin.recv(100000, socket.MSG_DONTWAIT) except socket.BlockError: p = None @@ -278,7 +278,7 @@ def stream_test(): len(''.join(D_got)) == len(''.join(D_recv)): break - if time.clock() >= end_time: + if time.time() >= end_time: may_need_increase = True C_got = ''.join(C_got)