Skip to content

Commit c50777c

Browse files
committed
fix: improve listener performance, remove timeout in listener
1 parent 8a64648 commit c50777c

File tree

2 files changed

+14
-32
lines changed

2 files changed

+14
-32
lines changed

pytest-embedded/pytest_embedded/dut_factory.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,45 +50,40 @@ def msg_queue_gn() -> MessageQueue:
5050

5151

5252
def _listen(q: MessageQueue, filepath: str, with_timestamp: bool = True, count: int = 1, total: int = 1) -> None:
53-
_added_prefix = False
53+
shall_add_prefix = True
5454
while True:
55-
msgs = q.get_all()
56-
if not msgs:
55+
msg = q.get()
56+
if not msg:
5757
continue
5858

59-
msg_b = b''.join(msgs)
6059
with open(filepath, 'ab') as fw:
61-
fw.write(msg_b)
60+
fw.write(msg)
6261
fw.flush()
6362

64-
_s = to_str(msg_b)
63+
_s = to_str(msg)
6564
if not _s:
6665
continue
6766

6867
prefix = ''
6968
if total > 1:
70-
source = f'dut-{count}'
71-
else:
72-
source = None
73-
74-
if source:
75-
prefix = f'[{source}] ' + prefix
69+
prefix = f'[dut-{count}] '
7670

7771
if with_timestamp:
7872
prefix = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ' ' + prefix
7973

80-
if not _added_prefix:
74+
if shall_add_prefix:
8175
_s = prefix + _s
82-
_added_prefix = True
76+
8377
_s = _s.replace('\r\n', '\n') # remove extra \r. since multi-dut \r would mess up the log
84-
_s = _s.replace('\n', '\n' + prefix)
85-
if prefix and _s.endswith(prefix):
86-
_s = _s.rsplit(prefix, maxsplit=1)[0]
87-
_added_prefix = False
78+
if _s.endswith('\n'): # complete line
79+
shall_add_prefix = True
80+
_s = _s[:-1].replace('\n', '\n' + prefix) + '\n'
81+
else:
82+
shall_add_prefix = False
83+
_s = _s.replace('\n', '\n' + prefix)
8884

8985
_stdout.write(_s)
9086
_stdout.flush()
91-
time.sleep(0.05)
9287

9388

9489
def _listener_gn(msg_queue, _pexpect_logfile, with_timestamp, dut_index, dut_total) -> multiprocessing.Process:

pytest-embedded/pytest_embedded/log.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import textwrap
99
import uuid
1010
from multiprocessing import queues
11-
from queue import Empty
1211
from typing import AnyStr, List, Optional, Union
1312

1413
import pexpect.fdpexpect
@@ -28,7 +27,6 @@ def __init__(self, *args, **kwargs):
2827
if 'ctx' not in kwargs:
2928
kwargs['ctx'] = _ctx
3029

31-
self.lock = _ctx.Lock()
3230
super().__init__(*args, **kwargs)
3331

3432
def put(self, obj, **kwargs):
@@ -45,17 +43,6 @@ def put(self, obj, **kwargs):
4543
except: # noqa # queue might be closed
4644
pass
4745

48-
def get_all(self) -> List[bytes]:
49-
res = []
50-
with self.lock:
51-
while True:
52-
try:
53-
res.append(self.get_nowait())
54-
except Empty:
55-
break
56-
57-
return res
58-
5946
def write(self, s: AnyStr):
6047
self.put(s)
6148

0 commit comments

Comments
 (0)