/usr/lib/python3/dist-packages/molotov/sharedconsole.py is in python3-molotov 1.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import sys
import asyncio
import multiprocessing
import os
from queue import Empty
from molotov.util import cancellable_sleep, printable_error
class SharedConsole(object):
"""Multi-process compatible stdout console.
"""
def __init__(self, interval=.1, max_lines_displayed=20):
self._stream = multiprocessing.Queue()
self._interval = interval
self._stop = True
self._creator = os.getpid()
self._stop = False
self._max_lines_displayed = max_lines_displayed
async def stop(self):
self._stop = True
while True:
try:
sys.stdout.write(self._stream.get_nowait())
except Empty:
break
sys.stdout.flush()
async def flush(self):
sys.stdout.flush()
await asyncio.sleep(0)
async def display(self):
if os.getpid() != self._creator:
return
while not self._stop:
lines_displayed = 0
while True:
try:
line = self._stream.get_nowait()
sys.stdout.write(line)
lines_displayed += 1
except Empty:
break
if self._stop or lines_displayed > self._max_lines_displayed:
break
else:
await asyncio.sleep(0)
sys.stdout.flush()
if not self._stop:
await cancellable_sleep(self._interval)
def print(self, line, end='\n'):
if os.getpid() != self._creator:
line = "[%d] %s" % (os.getpid(), line)
line += end
self._stream.put_nowait(line)
def print_error(self, error, tb=None):
for line in printable_error(error, tb):
self.print(line)
def print_block(self, start, callable, end='OK'):
if os.getpid() != self._creator:
prefix = "[%d] " % os.getpid()
else:
prefix = ''
self._stream.put(prefix + start + '...\n')
res = callable()
self._stream.put(prefix + 'OK\n')
return res
|