python: Reformat with Black, except quotes.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-02-11 23:19:30 -08:00
committed by Tim Abbott
parent 5028c081cb
commit 11741543da
817 changed files with 44952 additions and 24860 deletions

View File

@@ -23,29 +23,29 @@ logger = logging.getLogger('zulip.debug')
def interactive_debug(sig: int, frame: FrameType) -> None:
"""Interrupt running process, and provide a python prompt for
interactive debugging."""
d = {'_frame': frame} # Allow access to frame object.
d = {'_frame': frame} # Allow access to frame object.
d.update(frame.f_globals) # Unless shadowed by global
d.update(frame.f_locals)
message = "Signal received : entering python shell.\nTraceback:\n"
message = "Signal received : entering python shell.\nTraceback:\n"
message += ''.join(traceback.format_stack(frame))
i = code.InteractiveConsole(d)
i.interact(message)
# SIGUSR1 => Just print the stack
# SIGUSR2 => Print stack + open interactive debugging shell
def interactive_debug_listen() -> None:
signal.signal(signal.SIGUSR1, lambda sig, stack: traceback.print_stack(stack))
signal.signal(signal.SIGUSR2, interactive_debug)
def tracemalloc_dump() -> None:
if not tracemalloc.is_tracing():
logger.warning("pid %s: tracemalloc off, nothing to dump",
os.getpid())
logger.warning("pid %s: tracemalloc off, nothing to dump", os.getpid())
return
# Despite our name for it, `timezone_now` always deals in UTC.
basename = "snap.{}.{}".format(os.getpid(),
timezone_now().strftime("%F-%T"))
basename = "snap.{}.{}".format(os.getpid(), timezone_now().strftime("%F-%T"))
path = os.path.join(settings.TRACEMALLOC_DUMP_DIR, basename)
os.makedirs(settings.TRACEMALLOC_DUMP_DIR, exist_ok=True)
@@ -55,12 +55,15 @@ def tracemalloc_dump() -> None:
with open(f'/proc/{os.getpid()}/stat', 'rb') as f:
procstat = f.read().split()
rss_pages = int(procstat[23])
logger.info("tracemalloc dump: tracing %s MiB (%s MiB peak), using %s MiB; rss %s MiB; dumped %s",
tracemalloc.get_traced_memory()[0] // 1048576,
tracemalloc.get_traced_memory()[1] // 1048576,
tracemalloc.get_tracemalloc_memory() // 1048576,
rss_pages // 256,
basename)
logger.info(
"tracemalloc dump: tracing %s MiB (%s MiB peak), using %s MiB; rss %s MiB; dumped %s",
tracemalloc.get_traced_memory()[0] // 1048576,
tracemalloc.get_traced_memory()[1] // 1048576,
tracemalloc.get_tracemalloc_memory() // 1048576,
rss_pages // 256,
basename,
)
def tracemalloc_listen_sock(sock: socket.socket) -> None:
logger.debug('pid %s: tracemalloc_listen_sock started!', os.getpid())
@@ -68,8 +71,10 @@ def tracemalloc_listen_sock(sock: socket.socket) -> None:
sock.recv(1)
tracemalloc_dump()
listener_pid: Optional[int] = None
def tracemalloc_listen() -> None:
global listener_pid
if listener_pid == os.getpid():
@@ -81,13 +86,13 @@ def tracemalloc_listen() -> None:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
path = f"/tmp/tracemalloc.{os.getpid()}"
sock.bind(path)
thread = threading.Thread(target=lambda: tracemalloc_listen_sock(sock),
daemon=True)
thread = threading.Thread(target=lambda: tracemalloc_listen_sock(sock), daemon=True)
thread.start()
logger.debug('pid %s: tracemalloc_listen done: %s', os.getpid(), path)
def maybe_tracemalloc_listen() -> None:
'''If tracemalloc tracing enabled, listen for requests to dump a snapshot.
"""If tracemalloc tracing enabled, listen for requests to dump a snapshot.
To trigger once this is listening:
echo | socat -u stdin unix-sendto:/tmp/tracemalloc.$pid
@@ -101,7 +106,7 @@ def maybe_tracemalloc_listen() -> None:
https://docs.python.org/3/library/tracemalloc .
You may also have to add a call to this function somewhere.
'''
"""
if os.environ.get('PYTHONTRACEMALLOC'):
# If the server was started with `tracemalloc` tracing on, then
# listen for a signal to dump `tracemalloc` snapshots.