MCPcopy Create free account
hub / github.com/apify/crawlee-python / __aexit__

Method __aexit__

src/crawlee/events/_event_manager.py:113–143  ·  view source on GitHub ↗

Close the local event manager upon exiting the async context. This will stop listening for the events, and it will wait for all the event listeners to finish. Raises: RuntimeError: If the context manager is not active.

(
        self,
        exc_type: type[BaseException] | None,
        exc_value: BaseException | None,
        exc_traceback: TracebackType | None,
    )

Source from the content-addressed store, hash-verified

111 return self
112
113 async def __aexit__(
114 self,
115 exc_type: type[BaseException] | None,
116 exc_value: BaseException | None,
117 exc_traceback: TracebackType | None,
118 ) -> None:
119 """Close the local event manager upon exiting the async context.
120
121 This will stop listening for the events, and it will wait for all the event listeners to finish.
122
123 Raises:
124 RuntimeError: If the context manager is not active.
125 """
126 if not self.active:
127 raise RuntimeError(f'The {self.__class__.__name__} is not active.')
128
129 if self._active_ref_count > 1:
130 # Emit persist state event to ensure the latest state is saved before closing the context.
131 await self._emit_persist_state_event()
132 await self.wait_for_all_listeners_to_complete(timeout=self._close_timeout)
133 self._active_ref_count -= 1
134 return
135
136 # Stop persist state event periodic emission and manually emit last one to ensure latest state is saved.
137 await self._emit_persist_state_event_rec_task.stop()
138 await self._emit_persist_state_event()
139 await self.wait_for_all_listeners_to_complete(timeout=self._close_timeout)
140 self._event_emitter.remove_all_listeners()
141 self._listener_tasks.clear()
142 self._listeners_to_wrappers.clear()
143 self._active_ref_count -= 1
144
145 @overload
146 def on(self, *, event: Literal[Event.PERSIST_STATE], listener: EventListener[EventPersistStateData]) -> None: ...

Callers

nothing calls this directly

Calls 4

stopMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected