Skip to content

Connection Managers

CommandsManager

pydoll.connection.managers.commands_manager.CommandsManager

CommandsManager()

Manages command lifecycle and ID assignment for CDP commands.

Handles command future creation, ID generation, and response resolution for asynchronous command execution.

Initialize command manager with empty state.

create_command_future

create_command_future(command)

Create future for command and assign unique ID.

PARAMETER DESCRIPTION
command

Command to prepare for execution.

TYPE: Command

RETURNS DESCRIPTION
Future

Future that resolves when command completes.

resolve_command

resolve_command(response_id, result)

Resolve pending command with its result.

A late response can arrive after the caller already timed out (its future was cancelled by asyncio.wait_for) but before the command was removed. Popping and checking done() avoids InvalidStateError in that window.

remove_pending_command

remove_pending_command(command_id)

Remove pending command without resolving (for timeouts/cancellations).

fail_all_pending

fail_all_pending(exc)

Fail every pending command future with the given exception and clear them.

Used when the connection is lost so in-flight commands raise immediately instead of hanging until their individual timeout expires.

EventsManager

pydoll.connection.managers.events_manager.EventsManager

EventsManager()

Manages event callbacks, processing, and network logs.

Handles event callback registration, triggering, and maintains state for network logs and dialog information.

Initialize events manager with empty state.

network_logs instance-attribute

network_logs = deque(maxlen=MAX_NETWORK_LOGS)

dialog instance-attribute

start

start()

Start the event-processing worker if not already running.

Called when a connection is established. The worker drains queued events in arrival order so event handling never blocks the WebSocket read loop. Its lifetime is bound to the connection: stop() is invoked from the receive loop's finally block, so the worker can never outlive the socket.

enqueue_event

enqueue_event(event_data)

Queue an event for ordered, non-blocking processing.

Queuing is O(1) and never awaits, so a slow event callback can never delay delivery of command responses on the read loop.

stop async

stop()

Cancel the worker, await its exit, and drop any queued events.

Awaiting the cancelled task is what prevents "Task was destroyed but it is pending" warnings: the task is never garbage-collected while pending.

register_callback

register_callback(event_name, callback, temporary=False)

Register callback for specific event type.

PARAMETER DESCRIPTION
event_name

Event name to listen for.

TYPE: str

callback

Function called when event occurs.

TYPE: Callable[[dict], Any]

temporary

If True, callback removed after first trigger.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
int

Callback ID for later removal.

remove_callback

remove_callback(callback_id)

Remove callback by ID.

clear_callbacks

clear_callbacks()

Remove all registered callbacks.

process_event async

process_event(event_data)

Process received event and trigger callbacks.

Handles special events (network requests, dialogs) and updates internal state before triggering registered callbacks.