if __name__=="__main__": process_a = Process('a') process_a.go()
-------------------------------------------------------------- python.exe test.py Got a signal sent by <Process a> processing a
Process finished with exit code 0
从blinker引入一个signal类,生成了一个“ready”的信号,并使用connect方法将这个信号实例与subscriber方法绑定。之后在Process类中,生成了一个相同名字的信号,根据blinker的机制,这两个信号是同一个实例,也就是说,signal使用的是单例模式。然后使用ready的send方法放出信号,这时候与ready信号绑定的subscriber方法触发,输出“Got a signal sent by ”,之后输出“processing a”。
defb_subscriber(sender): print("Caught the signal from process_b") assert sender.name == 'b'
if __name__=="__main__": process_b = Process('b') ready.connect(b_subscriber,sender=process_b) process_b.go() process_a = Process('a') process_a.go() print(ready.receivers) ------------------------------------------------------------------------ Got a signal sent by <Process b> Caught the signal from process_b processing b complete a signal sent by <Process b> Got a signal sent by <Process a> processing a complete a signal sent by <Process a> {6061864: <weakref at 0x00000000010F80A8; to 'function' at 0x00000000005C7F28 (subscriber)>, 11257784: <weakref at 0x000000000111FA68; to 'function' at 0x0000000000ABC7B8 (b_subscriber)>}