Skip to content

Commit

Permalink
Merge pull request #337 from espressif/fix/get-queue-blocked
Browse files Browse the repository at this point in the history
fix: read all chunks in queue since we have interval in listener
  • Loading branch information
hfudev authored Jan 24, 2025
2 parents 0b20d32 + 748ac58 commit 8a64648
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pytest-embedded/pytest_embedded/dut_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ def msg_queue_gn() -> MessageQueue:
def _listen(q: MessageQueue, filepath: str, with_timestamp: bool = True, count: int = 1, total: int = 1) -> None:
_added_prefix = False
while True:
msg = q.get()
if not msg:
msgs = q.get_all()
if not msgs:
continue

msg_b = b''.join(msgs)
with open(filepath, 'ab') as fw:
fw.write(msg)
fw.write(msg_b)
fw.flush()

_s = to_str(msg)
_s = to_str(msg_b)
if not _s:
continue

Expand All @@ -87,7 +88,7 @@ def _listen(q: MessageQueue, filepath: str, with_timestamp: bool = True, count:

_stdout.write(_s)
_stdout.flush()
time.sleep(0.1)
time.sleep(0.05)


def _listener_gn(msg_queue, _pexpect_logfile, with_timestamp, dut_index, dut_total) -> multiprocessing.Process:
Expand Down
14 changes: 14 additions & 0 deletions pytest-embedded/pytest_embedded/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import textwrap
import uuid
from multiprocessing import queues
from queue import Empty
from typing import AnyStr, List, Optional, Union

import pexpect.fdpexpect
Expand All @@ -26,6 +27,8 @@ class MessageQueue(queues.Queue):
def __init__(self, *args, **kwargs):
if 'ctx' not in kwargs:
kwargs['ctx'] = _ctx

self.lock = _ctx.Lock()
super().__init__(*args, **kwargs)

def put(self, obj, **kwargs):
Expand All @@ -42,6 +45,17 @@ def put(self, obj, **kwargs):
except: # noqa # queue might be closed
pass

def get_all(self) -> List[bytes]:
res = []
with self.lock:
while True:
try:
res.append(self.get_nowait())
except Empty:
break

return res

def write(self, s: AnyStr):
self.put(s)

Expand Down

0 comments on commit 8a64648

Please sign in to comment.