base.py 16 KB

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