print() can deadlock 👁👄👁

This commit is contained in:
ed 2019-07-03 22:25:51 +00:00
parent 0cfda684ce
commit ea7c82c5e4
4 changed files with 34 additions and 23 deletions

View file

@ -150,6 +150,7 @@ class BrokerMp(object):
if msg != last:
last = msg
print(msg)
with self.hub.log_mutex:
print(msg)
time.sleep(0.1)

View file

@ -614,7 +614,7 @@ class HttpCli(object):
abspath = vn.canonical(rem)
if not os.path.exists(fsenc(abspath)):
print(abspath)
# print(abspath)
raise Pebkac(404)
if not os.path.isdir(fsenc(abspath)):

View file

@ -48,7 +48,7 @@ class HttpSrv(object):
return len(self.clients)
def shutdown(self):
print("ok bye")
self.log("ok bye")
def thr_client(self, sck, addr):
"""thread managing one tcp client"""
@ -69,13 +69,20 @@ class HttpSrv(object):
finally:
self.log(str(addr), "-" * 7 + "C-done")
sck.shutdown(socket.SHUT_RDWR)
sck.close()
with self.mutex:
del self.clients[cli]
try:
sck.shutdown(socket.SHUT_RDWR)
sck.close()
except (OSError, socket.error) as ex:
if ex.errno not in [107, 9]:
# 107 Transport endpoint not connected
# 9 Bad file descriptor
raise
finally:
with self.mutex:
del self.clients[cli]
if self.disconnect_func:
self.disconnect_func(addr) # pylint: disable=not-callable
if self.disconnect_func:
self.disconnect_func(addr) # pylint: disable=not-callable
def thr_workload(self):
"""indicates the python interpreter workload caused by this HttpSrv"""

View file

@ -52,28 +52,31 @@ class SvcHub(object):
try:
while True:
time.sleep(9001)
except KeyboardInterrupt:
print("OPYTHAT")
with self.log_mutex:
print("OPYTHAT")
self.tcpsrv.shutdown()
self.broker.shutdown()
print("nailed it")
def log(self, src, msg):
"""handles logging from all components"""
now = time.time()
if now >= self.next_day:
dt = datetime.utcfromtimestamp(now)
print("\033[36m{}\033[0m".format(dt.strftime("%Y-%m-%d")))
# unix timestamp of next 00:00:00 (leap-seconds safe)
day_now = dt.day
while dt.day == day_now:
dt += timedelta(hours=12)
dt = dt.replace(hour=0, minute=0, second=0)
self.next_day = calendar.timegm(dt.utctimetuple())
with self.log_mutex:
now = time.time()
if now >= self.next_day:
dt = datetime.utcfromtimestamp(now)
print("\033[36m{}\033[0m".format(dt.strftime("%Y-%m-%d")))
# unix timestamp of next 00:00:00 (leap-seconds safe)
day_now = dt.day
while dt.day == day_now:
dt += timedelta(hours=12)
dt = dt.replace(hour=0, minute=0, second=0)
self.next_day = calendar.timegm(dt.utctimetuple())
ts = datetime.utcfromtimestamp(now).strftime("%H:%M:%S")
print("\033[36m{} \033[33m{:21} \033[0m{}".format(ts, src, msg))