base.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. """Signals and events.
  2. A small implementation of signals, inspired by a snippet of Django signal
  3. API client code seen in a blog post. Signals are first-class objects and
  4. each manages its own receivers and message emission.
  5. The :func:`signal` function provides singleton behavior for named signals.
  6. """
  7. from __future__ import annotations
  8. import typing as t
  9. from collections import defaultdict
  10. from contextlib import contextmanager
  11. from warnings import warn
  12. from weakref import WeakValueDictionary
  13. from blinker._utilities import annotatable_weakref
  14. from blinker._utilities import hashable_identity
  15. from blinker._utilities import IdentityType
  16. from blinker._utilities import is_coroutine_function
  17. from blinker._utilities import lazy_property
  18. from blinker._utilities import reference
  19. from blinker._utilities import symbol
  20. from blinker._utilities import WeakTypes
  21. if t.TYPE_CHECKING:
  22. import typing_extensions as te
  23. T_callable = t.TypeVar("T_callable", bound=t.Callable[..., t.Any])
  24. T = t.TypeVar("T")
  25. P = te.ParamSpec("P")
  26. AsyncWrapperType = t.Callable[[t.Callable[P, t.Awaitable[T]]], t.Callable[P, T]]
  27. SyncWrapperType = t.Callable[[t.Callable[P, T]], t.Callable[P, t.Awaitable[T]]]
  28. ANY = symbol("ANY")
  29. ANY.__doc__ = 'Token for "any sender".'
  30. ANY_ID = 0
  31. class Signal:
  32. """A notification emitter."""
  33. #: An :obj:`ANY` convenience synonym, allows ``Signal.ANY``
  34. #: without an additional import.
  35. ANY = ANY
  36. @lazy_property
  37. def receiver_connected(self) -> Signal:
  38. """Emitted after each :meth:`connect`.
  39. The signal sender is the signal instance, and the :meth:`connect`
  40. arguments are passed through: *receiver*, *sender*, and *weak*.
  41. .. versionadded:: 1.2
  42. """
  43. return Signal(doc="Emitted after a receiver connects.")
  44. @lazy_property
  45. def receiver_disconnected(self) -> Signal:
  46. """Emitted after :meth:`disconnect`.
  47. The sender is the signal instance, and the :meth:`disconnect` arguments
  48. are passed through: *receiver* and *sender*.
  49. Note, this signal is emitted **only** when :meth:`disconnect` is
  50. called explicitly.
  51. The disconnect signal can not be emitted by an automatic disconnect
  52. (due to a weakly referenced receiver or sender going out of scope),
  53. as the receiver and/or sender instances are no longer available for
  54. use at the time this signal would be emitted.
  55. An alternative approach is available by subscribing to
  56. :attr:`receiver_connected` and setting up a custom weakref cleanup
  57. callback on weak receivers and senders.
  58. .. versionadded:: 1.2
  59. """
  60. return Signal(doc="Emitted after a receiver disconnects.")
  61. def __init__(self, doc: str | None = None) -> None:
  62. """
  63. :param doc: optional. If provided, will be assigned to the signal's
  64. __doc__ attribute.
  65. """
  66. if doc:
  67. self.__doc__ = doc
  68. #: A mapping of connected receivers.
  69. #:
  70. #: The values of this mapping are not meaningful outside of the
  71. #: internal :class:`Signal` implementation, however the boolean value
  72. #: of the mapping is useful as an extremely efficient check to see if
  73. #: any receivers are connected to the signal.
  74. self.receivers: dict[IdentityType, t.Callable | annotatable_weakref] = {}
  75. self.is_muted = False
  76. self._by_receiver: dict[IdentityType, set[IdentityType]] = defaultdict(set)
  77. self._by_sender: dict[IdentityType, set[IdentityType]] = defaultdict(set)
  78. self._weak_senders: dict[IdentityType, annotatable_weakref] = {}
  79. def connect(
  80. self, receiver: T_callable, sender: t.Any = ANY, weak: bool = True
  81. ) -> T_callable:
  82. """Connect *receiver* to signal events sent by *sender*.
  83. :param receiver: A callable. Will be invoked by :meth:`send` with
  84. `sender=` as a single positional argument and any ``kwargs`` that
  85. were provided to a call to :meth:`send`.
  86. :param sender: Any object or :obj:`ANY`, defaults to ``ANY``.
  87. Restricts notifications delivered to *receiver* to only those
  88. :meth:`send` emissions sent by *sender*. If ``ANY``, the receiver
  89. will always be notified. A *receiver* may be connected to
  90. multiple *sender* values on the same Signal through multiple calls
  91. to :meth:`connect`.
  92. :param weak: If true, the Signal will hold a weakref to *receiver*
  93. and automatically disconnect when *receiver* goes out of scope or
  94. is garbage collected. Defaults to True.
  95. """
  96. receiver_id = hashable_identity(receiver)
  97. receiver_ref: T_callable | annotatable_weakref
  98. if weak:
  99. receiver_ref = reference(receiver, self._cleanup_receiver)
  100. receiver_ref.receiver_id = receiver_id
  101. else:
  102. receiver_ref = receiver
  103. sender_id: IdentityType
  104. if sender is ANY:
  105. sender_id = ANY_ID
  106. else:
  107. sender_id = hashable_identity(sender)
  108. self.receivers.setdefault(receiver_id, receiver_ref)
  109. self._by_sender[sender_id].add(receiver_id)
  110. self._by_receiver[receiver_id].add(sender_id)
  111. del receiver_ref
  112. if sender is not ANY and sender_id not in self._weak_senders:
  113. # wire together a cleanup for weakref-able senders
  114. try:
  115. sender_ref = reference(sender, self._cleanup_sender)
  116. sender_ref.sender_id = sender_id
  117. except TypeError:
  118. pass
  119. else:
  120. self._weak_senders.setdefault(sender_id, sender_ref)
  121. del sender_ref
  122. # broadcast this connection. if receivers raise, disconnect.
  123. if "receiver_connected" in self.__dict__ and self.receiver_connected.receivers:
  124. try:
  125. self.receiver_connected.send(
  126. self, receiver=receiver, sender=sender, weak=weak
  127. )
  128. except TypeError as e:
  129. self.disconnect(receiver, sender)
  130. raise e
  131. if receiver_connected.receivers and self is not receiver_connected:
  132. try:
  133. receiver_connected.send(
  134. self, receiver_arg=receiver, sender_arg=sender, weak_arg=weak
  135. )
  136. except TypeError as e:
  137. self.disconnect(receiver, sender)
  138. raise e
  139. return receiver
  140. def connect_via(
  141. self, sender: t.Any, weak: bool = False
  142. ) -> t.Callable[[T_callable], T_callable]:
  143. """Connect the decorated function as a receiver for *sender*.
  144. :param sender: Any object or :obj:`ANY`. The decorated function
  145. will only receive :meth:`send` emissions sent by *sender*. If
  146. ``ANY``, the receiver will always be notified. A function may be
  147. decorated multiple times with differing *sender* values.
  148. :param weak: If true, the Signal will hold a weakref to the
  149. decorated function and automatically disconnect when *receiver*
  150. goes out of scope or is garbage collected. Unlike
  151. :meth:`connect`, this defaults to False.
  152. The decorated function will be invoked by :meth:`send` with
  153. `sender=` as a single positional argument and any ``kwargs`` that
  154. were provided to the call to :meth:`send`.
  155. .. versionadded:: 1.1
  156. """
  157. def decorator(fn: T_callable) -> T_callable:
  158. self.connect(fn, sender, weak)
  159. return fn
  160. return decorator
  161. @contextmanager
  162. def connected_to(
  163. self, receiver: t.Callable, sender: t.Any = ANY
  164. ) -> t.Generator[None, None, None]:
  165. """Execute a block with the signal temporarily connected to *receiver*.
  166. :param receiver: a receiver callable
  167. :param sender: optional, a sender to filter on
  168. This is a context manager for use in the ``with`` statement. It can
  169. be useful in unit tests. *receiver* is connected to the signal for
  170. the duration of the ``with`` block, and will be disconnected
  171. automatically when exiting the block:
  172. .. code-block:: python
  173. with on_ready.connected_to(receiver):
  174. # do stuff
  175. on_ready.send(123)
  176. .. versionadded:: 1.1
  177. """
  178. self.connect(receiver, sender=sender, weak=False)
  179. try:
  180. yield None
  181. finally:
  182. self.disconnect(receiver)
  183. @contextmanager
  184. def muted(self) -> t.Generator[None, None, None]:
  185. """Context manager for temporarily disabling signal.
  186. Useful for test purposes.
  187. """
  188. self.is_muted = True
  189. try:
  190. yield None
  191. except Exception as e:
  192. raise e
  193. finally:
  194. self.is_muted = False
  195. def temporarily_connected_to(
  196. self, receiver: t.Callable, sender: t.Any = ANY
  197. ) -> t.ContextManager[None]:
  198. """An alias for :meth:`connected_to`.
  199. :param receiver: a receiver callable
  200. :param sender: optional, a sender to filter on
  201. .. versionadded:: 0.9
  202. .. versionchanged:: 1.1
  203. Renamed to :meth:`connected_to`. ``temporarily_connected_to`` was
  204. deprecated in 1.2 and will be removed in a subsequent version.
  205. """
  206. warn(
  207. "temporarily_connected_to is deprecated; use connected_to instead.",
  208. DeprecationWarning,
  209. )
  210. return self.connected_to(receiver, sender)
  211. def send(
  212. self,
  213. *sender: t.Any,
  214. _async_wrapper: AsyncWrapperType | None = None,
  215. **kwargs: t.Any,
  216. ) -> list[tuple[t.Callable, t.Any]]:
  217. """Emit this signal on behalf of *sender*, passing on ``kwargs``.
  218. Returns a list of 2-tuples, pairing receivers with their return
  219. value. The ordering of receiver notification is undefined.
  220. :param sender: Any object or ``None``. If omitted, synonymous
  221. with ``None``. Only accepts one positional argument.
  222. :param _async_wrapper: A callable that should wrap a coroutine
  223. receiver and run it when called synchronously.
  224. :param kwargs: Data to be sent to receivers.
  225. """
  226. if self.is_muted:
  227. return []
  228. sender = self._extract_sender(sender)
  229. results = []
  230. for receiver in self.receivers_for(sender):
  231. if is_coroutine_function(receiver):
  232. if _async_wrapper is None:
  233. raise RuntimeError("Cannot send to a coroutine function")
  234. receiver = _async_wrapper(receiver)
  235. result = receiver(sender, **kwargs)
  236. results.append((receiver, result))
  237. return results
  238. async def send_async(
  239. self,
  240. *sender: t.Any,
  241. _sync_wrapper: SyncWrapperType | None = None,
  242. **kwargs: t.Any,
  243. ) -> list[tuple[t.Callable, t.Any]]:
  244. """Emit this signal on behalf of *sender*, passing on ``kwargs``.
  245. Returns a list of 2-tuples, pairing receivers with their return
  246. value. The ordering of receiver notification is undefined.
  247. :param sender: Any object or ``None``. If omitted, synonymous
  248. with ``None``. Only accepts one positional argument.
  249. :param _sync_wrapper: A callable that should wrap a synchronous
  250. receiver and run it when awaited.
  251. :param kwargs: Data to be sent to receivers.
  252. """
  253. if self.is_muted:
  254. return []
  255. sender = self._extract_sender(sender)
  256. results = []
  257. for receiver in self.receivers_for(sender):
  258. if not is_coroutine_function(receiver):
  259. if _sync_wrapper is None:
  260. raise RuntimeError("Cannot send to a non-coroutine function")
  261. receiver = _sync_wrapper(receiver)
  262. result = await receiver(sender, **kwargs)
  263. results.append((receiver, result))
  264. return results
  265. def _extract_sender(self, sender: t.Any) -> t.Any:
  266. if not self.receivers:
  267. # Ensure correct signature even on no-op sends, disable with -O
  268. # for lowest possible cost.
  269. if __debug__ and sender and len(sender) > 1:
  270. raise TypeError(
  271. f"send() accepts only one positional argument, {len(sender)} given"
  272. )
  273. return []
  274. # Using '*sender' rather than 'sender=None' allows 'sender' to be
  275. # used as a keyword argument- i.e. it's an invisible name in the
  276. # function signature.
  277. if len(sender) == 0:
  278. sender = None
  279. elif len(sender) > 1:
  280. raise TypeError(
  281. f"send() accepts only one positional argument, {len(sender)} given"
  282. )
  283. else:
  284. sender = sender[0]
  285. return sender
  286. def has_receivers_for(self, sender: t.Any) -> bool:
  287. """True if there is probably a receiver for *sender*.
  288. Performs an optimistic check only. Does not guarantee that all
  289. weakly referenced receivers are still alive. See
  290. :meth:`receivers_for` for a stronger search.
  291. """
  292. if not self.receivers:
  293. return False
  294. if self._by_sender[ANY_ID]:
  295. return True
  296. if sender is ANY:
  297. return False
  298. return hashable_identity(sender) in self._by_sender
  299. def receivers_for(
  300. self, sender: t.Any
  301. ) -> t.Generator[t.Callable[[t.Any], t.Any], None, None]:
  302. """Iterate all live receivers listening for *sender*."""
  303. # TODO: test receivers_for(ANY)
  304. if self.receivers:
  305. sender_id = hashable_identity(sender)
  306. if sender_id in self._by_sender:
  307. ids = self._by_sender[ANY_ID] | self._by_sender[sender_id]
  308. else:
  309. ids = self._by_sender[ANY_ID].copy()
  310. for receiver_id in ids:
  311. receiver = self.receivers.get(receiver_id)
  312. if receiver is None:
  313. continue
  314. if isinstance(receiver, WeakTypes):
  315. strong = receiver()
  316. if strong is None:
  317. self._disconnect(receiver_id, ANY_ID)
  318. continue
  319. receiver = strong
  320. yield receiver # type: ignore[misc]
  321. def disconnect(self, receiver: t.Callable, sender: t.Any = ANY) -> None:
  322. """Disconnect *receiver* from this signal's events.
  323. :param receiver: a previously :meth:`connected<connect>` callable
  324. :param sender: a specific sender to disconnect from, or :obj:`ANY`
  325. to disconnect from all senders. Defaults to ``ANY``.
  326. """
  327. sender_id: IdentityType
  328. if sender is ANY:
  329. sender_id = ANY_ID
  330. else:
  331. sender_id = hashable_identity(sender)
  332. receiver_id = hashable_identity(receiver)
  333. self._disconnect(receiver_id, sender_id)
  334. if (
  335. "receiver_disconnected" in self.__dict__
  336. and self.receiver_disconnected.receivers
  337. ):
  338. self.receiver_disconnected.send(self, receiver=receiver, sender=sender)
  339. def _disconnect(self, receiver_id: IdentityType, sender_id: IdentityType) -> None:
  340. if sender_id == ANY_ID:
  341. if self._by_receiver.pop(receiver_id, False):
  342. for bucket in self._by_sender.values():
  343. bucket.discard(receiver_id)
  344. self.receivers.pop(receiver_id, None)
  345. else:
  346. self._by_sender[sender_id].discard(receiver_id)
  347. self._by_receiver[receiver_id].discard(sender_id)
  348. def _cleanup_receiver(self, receiver_ref: annotatable_weakref) -> None:
  349. """Disconnect a receiver from all senders."""
  350. self._disconnect(t.cast(IdentityType, receiver_ref.receiver_id), ANY_ID)
  351. def _cleanup_sender(self, sender_ref: annotatable_weakref) -> None:
  352. """Disconnect all receivers from a sender."""
  353. sender_id = t.cast(IdentityType, sender_ref.sender_id)
  354. assert sender_id != ANY_ID
  355. self._weak_senders.pop(sender_id, None)
  356. for receiver_id in self._by_sender.pop(sender_id, ()):
  357. self._by_receiver[receiver_id].discard(sender_id)
  358. def _cleanup_bookkeeping(self) -> None:
  359. """Prune unused sender/receiver bookkeeping. Not threadsafe.
  360. Connecting & disconnecting leave behind a small amount of bookkeeping
  361. for the receiver and sender values. Typical workloads using Blinker,
  362. for example in most web apps, Flask, CLI scripts, etc., are not
  363. adversely affected by this bookkeeping.
  364. With a long-running Python process performing dynamic signal routing
  365. with high volume- e.g. connecting to function closures, "senders" are
  366. all unique object instances, and doing all of this over and over- you
  367. may see memory usage will grow due to extraneous bookkeeping. (An empty
  368. set() for each stale sender/receiver pair.)
  369. This method will prune that bookkeeping away, with the caveat that such
  370. pruning is not threadsafe. The risk is that cleanup of a fully
  371. disconnected receiver/sender pair occurs while another thread is
  372. connecting that same pair. If you are in the highly dynamic, unique
  373. receiver/sender situation that has lead you to this method, that
  374. failure mode is perhaps not a big deal for you.
  375. """
  376. for mapping in (self._by_sender, self._by_receiver):
  377. for _id, bucket in list(mapping.items()):
  378. if not bucket:
  379. mapping.pop(_id, None)
  380. def _clear_state(self) -> None:
  381. """Throw away all signal state. Useful for unit tests."""
  382. self._weak_senders.clear()
  383. self.receivers.clear()
  384. self._by_sender.clear()
  385. self._by_receiver.clear()
  386. receiver_connected = Signal(
  387. """\
  388. Sent by a :class:`Signal` after a receiver connects.
  389. :argument: the Signal that was connected to
  390. :keyword receiver_arg: the connected receiver
  391. :keyword sender_arg: the sender to connect to
  392. :keyword weak_arg: true if the connection to receiver_arg is a weak reference
  393. .. deprecated:: 1.2
  394. As of 1.2, individual signals have their own private
  395. :attr:`~Signal.receiver_connected` and
  396. :attr:`~Signal.receiver_disconnected` signals with a slightly simplified
  397. call signature. This global signal is planned to be removed in 1.6.
  398. """
  399. )
  400. class NamedSignal(Signal):
  401. """A named generic notification emitter."""
  402. def __init__(self, name: str, doc: str | None = None) -> None:
  403. Signal.__init__(self, doc)
  404. #: The name of this signal.
  405. self.name = name
  406. def __repr__(self) -> str:
  407. base = Signal.__repr__(self)
  408. return f"{base[:-1]}; {self.name!r}>"
  409. class Namespace(dict):
  410. """A mapping of signal names to signals."""
  411. def signal(self, name: str, doc: str | None = None) -> NamedSignal:
  412. """Return the :class:`NamedSignal` *name*, creating it if required.
  413. Repeated calls to this function will return the same signal object.
  414. """
  415. try:
  416. return self[name] # type: ignore[no-any-return]
  417. except KeyError:
  418. result = self.setdefault(name, NamedSignal(name, doc))
  419. return result # type: ignore[no-any-return]
  420. class WeakNamespace(WeakValueDictionary):
  421. """A weak mapping of signal names to signals.
  422. Automatically cleans up unused Signals when the last reference goes out
  423. of scope. This namespace implementation exists for a measure of legacy
  424. compatibility with Blinker <= 1.2, and may be dropped in the future.
  425. .. versionadded:: 1.3
  426. """
  427. def signal(self, name: str, doc: str | None = None) -> NamedSignal:
  428. """Return the :class:`NamedSignal` *name*, creating it if required.
  429. Repeated calls to this function will return the same signal object.
  430. """
  431. try:
  432. return self[name] # type: ignore[no-any-return]
  433. except KeyError:
  434. result = self.setdefault(name, NamedSignal(name, doc))
  435. return result # type: ignore[no-any-return]
  436. signal = Namespace().signal