From b20ec87c705941f4dcafa2c579264535bd2742e3 Mon Sep 17 00:00:00 2001
From: Oleksii Shevchuk
Date: Fri, 26 Aug 2016 12:36:32 +0300
Subject: [PATCH] Simplify polling
---
pupy/modules/interactive_shell.py | 5 ++---
pupy/network/lib/servers.py | 18 ++++++------------
pupy/pp.py | 23 ++++++++---------------
3 files changed, 16 insertions(+), 30 deletions(-)
diff --git a/pupy/modules/interactive_shell.py b/pupy/modules/interactive_shell.py
index dda1b088..5a4ae0ed 100644
--- a/pupy/modules/interactive_shell.py
+++ b/pupy/modules/interactive_shell.py
@@ -80,8 +80,9 @@ class InteractiveShell(PupyModule):
try:
tty.setraw(fd)
buf=b''
+ print self.client.conn.__dict__
while True:
- r, w, x = select.select([sys.stdin], [], [], 0)
+ r, w, x = select.select([sys.stdin], [], [], 0.01)
if sys.stdin in r:
ch = os.read(fd, 1)
buf += ch
@@ -90,8 +91,6 @@ class InteractiveShell(PupyModule):
buf=b''
elif is_closed.is_set():
break
- else:
- time.sleep(0.01)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
pupylib.PupySignalHandler.set_signal_winch(old_handler)
diff --git a/pupy/network/lib/servers.py b/pupy/network/lib/servers.py
index 2a00a570..29600dd4 100644
--- a/pupy/network/lib/servers.py
+++ b/pupy/network/lib/servers.py
@@ -234,17 +234,13 @@ class PupyTCPServer(ThreadPoolServer):
h=addrinfo[0]
p=addrinfo[1]
config = dict(self.protocol_config, credentials=credentials, connid="%s:%d"%(h, p))
+
def check_timeout(event, cb, timeout=60):
- start_time=time.time()
- while True:
- if time.time()-start_time>timeout:
- if not event.is_set():
- logging.error("timeout occured !")
- cb()
- break
- elif event.is_set():
- break
- time.sleep(0.5)
+ time.sleep(timeout)
+ if not event.is_set():
+ logging.error("timeout occured !")
+ cb()
+
stream=self.stream_class(sock, self.transport_class, self.transport_kwargs)
event=multiprocessing.Event()
@@ -257,8 +253,6 @@ class PupyTCPServer(ThreadPoolServer):
event.set()
return c
-
-
class PupyUDPServer(object):
def __init__(self, service, **kwargs):
if not "stream" in kwargs:
diff --git a/pupy/pp.py b/pupy/pp.py
index ca172e55..c8aa5590 100755
--- a/pupy/pp.py
+++ b/pupy/pp.py
@@ -146,8 +146,6 @@ def get_next_wait(attempt):
else:
return random.randint(150,300)/10.0
-
-
def set_connect_back_host(HOST):
import pupy
pupy.get_connect_back_host=(lambda: HOST)
@@ -212,17 +210,13 @@ def rpyc_loop(launcher):
s.start()
else: # connect payload
stream=ret
+
def check_timeout(event, cb, timeout=60):
- start_time=time.time()
- while True:
- if time.time()-start_time>timeout:
- if not event.is_set():
- logging.error("timeout occured !")
- cb()
- break
- elif event.is_set():
- break
- time.sleep(0.5)
+ time.sleep(timeout)
+ if not event.is_set():
+ logging.error("timeout occured !")
+ cb()
+
event=threading.Event()
t=threading.Thread(target=check_timeout, args=(event, stream.close))
t.daemon=True
@@ -231,9 +225,8 @@ def rpyc_loop(launcher):
conn=rpyc.utils.factory.connect_stream(stream, ReverseSlaveService, {})
finally:
event.set()
- while not stream.closed:
- attempt=0
- conn.serve(0.01)
+ attempt=0
+ conn.serve_all()
except KeyboardInterrupt:
raise
except EOFError: