Skip to content

Commit

Permalink
Improve print outs
Browse files Browse the repository at this point in the history
  • Loading branch information
danijar committed Sep 5, 2024
1 parent e31caa4 commit 67831bc
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Fast and reliable distributed systems in Python.
## Features

- 📡 **Communication:** Portal lets you bind functions to a `Server` and call
them from a `Client`. Wait on results via `Future` objects. Clients can
automatically restore broken connections.
them from one or more `Client`s. Wait on results via `Future` objects.
Clients can automatically restore broken connections.
- 🚀 **Performance:** Optimized for throughput and latency. Array data is
zero-copy serialized and deserialized for throughput near the hardware limit.
- 🤸 **Flexibility:** Function inputs and outputs can be nested dicts and lists
Expand Down
2 changes: 1 addition & 1 deletion portal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '3.0.0'
__version__ = '3.1.0'

import multiprocessing as mp
try:
Expand Down
2 changes: 1 addition & 1 deletion portal/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class Client:

def __init__(
self, host, port, name='Client', maxinflight=16, **kwargs):
self, host, port=None, name='Client', maxinflight=16, **kwargs):
assert 1 <= maxinflight, maxinflight
self.maxinflight = maxinflight
self.reqnum = iter(itertools.count(0))
Expand Down
20 changes: 13 additions & 7 deletions portal/client_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Options:
keepalive_every: float = 10
keepalive_fails: int = 10
logging: bool = True
logging_color: str = 'yellow'


class ClientSocket:
Expand Down Expand Up @@ -175,6 +176,7 @@ def _connect(self):
once = True
while self.running:
sock, addr = self._create()
error = None
try:
# We need to resolve the address regularly.
if contextlib.context.resolver:
Expand All @@ -185,12 +187,12 @@ def _connect(self):
self._log('Connection established')
return sock
except ConnectionError as e:
self._log(f'Connection error ({e})')
error = e
time.sleep(0.1)
except TimeoutError:
pass
except TimeoutError as e:
error = e
if once:
self._log('Still trying to connect...')
self._log(f'Still trying to connect... ({error})')
once = False
sock.close()
return None
Expand Down Expand Up @@ -221,7 +223,11 @@ def _create(self):
return sock, addr

def _log(self, *args):
if self.options.logging:
style = utils.style(color='yellow', bold=True)
if not self.options.logging:
return
if self.options.logging_color:
style = utils.style(color=self.options.logging_color)
reset = utils.style(reset=True)
print(style + f'[{self.name}]', *args, reset)
else:
style, reset = '', ''
print(style + f'[{self.name}]' + reset, *args)
8 changes: 4 additions & 4 deletions portal/contextlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,18 @@ def get_children(self, ident=None):

def _watcher(self):
while True:
if self.done.wait(self.interval):
break
if self.errfile and self.errfile.exists():
print(f'Shutting down due to error file: {self.errfile}')
self.shutdown(2)
if self.done.wait(self.interval):
break


context = Context()


def initfn(fn):
context.initfn(fn)
def initfn(fn, call_now=True):
context.initfn(fn, call_now)


def setup(**kwargs):
Expand Down
11 changes: 8 additions & 3 deletions portal/server_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Options:
max_recv_queue: int = 4096
max_send_queue: int = 4096
logging: bool = True
logging_color: str = 'blue'


class ServerSocket:
Expand Down Expand Up @@ -161,7 +162,11 @@ def _numsending(self):
return sum(len(x.sendbufs) for x in self.conns.values())

def _log(self, *args, **kwargs):
if self.options.logging:
style = utils.style(color='blue', bold=True)
if not self.options.logging:
return
if self.options.logging_color:
style = utils.style(color=self.options.logging_color)
reset = utils.style(reset=True)
print(style + f'[{self.name}]', *args, reset)
else:
style, reset = '', ''
print(style + f'[{self.name}]' + reset, *args)

0 comments on commit 67831bc

Please sign in to comment.