Created
April 2, 2020 07:26
-
-
Save AntiKnot/1be46ce2b323b9e28aa550a468871481 to your computer and use it in GitHub Desktop.
python signal blinker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from blinker import signal | |
| def subscriber(sender): | |
| print("Got a signal send by %r" % sender) | |
| # already have signal, define own receive method | |
| send_data = signal('send-data') | |
| @send_data.connect | |
| def receive_data(sender, **kw): | |
| print("Caught signal from %r, data %r" % (sender, kw)) | |
| return 'received!' | |
| # already have receive method, define own signal | |
| def receive_data(sender, **kw): | |
| print("Caught signal from %r, data %r" % (sender, kw)) | |
| return 'received!' | |
| send_data = signal('send-data') | |
| send_data.connect(receive_data) | |
| # send a signal but only receive | |
| dice_roll = signal('dice_roll') | |
| another_dice_roll = signal('another_dice_roll') | |
| # subscriber muti signal with special mark. | |
| @dice_roll.connect_via(1) | |
| @dice_roll.connect_via(3) | |
| @another_dice_roll.connect_via(2) | |
| def odd_subscriber(sender): | |
| print("Observed dice roll %r." % sender) | |
| dice_roll.send(3) | |
| dice_roll.send(2) | |
| another_dice_roll.send(1) | |
| another_dice_roll.send(2) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
主要差别发布者和订阅者的绑定方式。根据功能的调用方向可以灵活使用。