Skip to content

Performance: pipewire_native.py uses time.sleep polling in find_nodes_by_pid #33

@m96-chan

Description

@m96-chan

Summary

The PipeWireNodeDiscovery class uses a busy-polling pattern with time.sleep instead of proper event-driven waiting.

Location

src/proctap/backends/pipewire_native.py:1027-1037

Problem

# Run main loop for a short time to discover nodes
# Note: This is a simplified approach; a proper implementation
# would use a timer to stop the loop after timeout
if self._pw._main_loop:
    import time
    start = time.time()
    while (time.time() - start) * 1000 < timeout_ms:
        # Give PipeWire time to process events
        time.sleep(0.01)  # 10ms polling
        # Break early if we found nodes
        if self._found_nodes:
            break

Impact

  • Wastes CPU cycles on polling
  • 10ms sleep granularity means potentially 10ms wasted latency
  • Does not actually process PipeWire events (no pw_loop_iterate call)
  • The comment itself acknowledges this is a "simplified approach"

Suggested Fix

Use PipeWire's native event loop:

# Use pw_loop_iterate to properly process events
loop = _pw_lib.pw_main_loop_get_loop(self._pw._main_loop)
while not self._found_nodes and elapsed_ms < timeout_ms:
    # Process pending events with small timeout
    _pw_lib.pw_loop_iterate(loop, 10)  # 10ms timeout

Or use a threading.Event that gets set from the callback:

self._found_event = threading.Event()
# In callback: self._found_event.set()
# In find_nodes_by_pid:
self._found_event.wait(timeout=timeout_ms / 1000)

Labels

performance, enhancement

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions