In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "how to create the blinker signal library of python". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to create the blinker signal library of python" can help you solve your doubts.
I. installation
A powerful Python-based semaphore that supports both simple object-to-object communication and multicast for multiple objects
Support for registering global named signals and custom named signals
Support for anonymous signals, thread safety
Support persistent and transient connections to the recipient
Automatic disconnection from the recipient through weak references
Support for sending data of any size and collecting the return value of the receiver of the signal
Pip install blinker II. Create and send signals
The signal is created by the signal () method
The signal is sent by the send () method
From blinker import signal# creates a signal and names it s = signal ('autofelix') def say (args): print (' I am Flying Rabbit') # signal registers a receiver s.connect (say) if "_ _ main__" = = _ _ name__:# send signal s.send () 3. Anonymous signal
Blinker also supports anonymous signals, but there is no need to specify a specific signal value.
Each anonymous signal created is independent of each other.
From blinker import signal# creates an anonymous signal s = signal () def say (args): print ('I am Flying Rabbit') # signal registers a receiver s.connect (say) if "_ _ main__" = = _ _ name__:# send signal s.send () 4. Multicast signal
Multicast signal is a characteristic that can reflect the advantages of signal.
Multiple receivers register with the signal, and the sender only needs to send the message once to transmit the information to multiple receivers.
From blinker import signals = signal ('autofelix') def fans_one (args): print (f' I am a fan number one, I follow: {args}') def fans_two (args): print (f'I am a fan number two, I follow: {args}') s.connect (fans_one) s.connect (fans_two) if "_ _ main__" = _ _ name__:s.send ('flying rabbit') V, recipient subscription topic
It is sent to the recipient only when the specified subject sends a message
From blinker import signals = signal ('autofelix') def fans (args): print (f' I am a little fan, {args} is my teacher') # signal registers a receiver and specifies the theme s.connect (animal, sender=' Flying Rabbit') if "_ main__" = _ _ name__:for I in ['Flying Rabbit', 'Flying Rabbit Little Sister', 'Flying Rabbit Little Brother']: s.send (I) VI, Decoration usage
In addition to function registration, there is a simpler method of signal registration, which is the decorator.
From blinker import signals = signal ('autofelix') @ s.connectdef fans_one (args): print (I am a fan number one, I follow: {args}') @ s.connectdef fans_two (args): print (f'I am a fan number two, I follow: {args}') if "_ main__" = = _ name__:s.send ('flying rabbit') 7. Subscribe theme decorator
One drawback of connect's registration method using decorators is that they cannot subscribe to topics.
The connect_via method supports subscribing to topics
From blinker import signals = signal ('autofelix') @ s.connect_via (' Flying Rabbit') def fans (args): print (f'I am a little fan, {args} is my teacher') if "_ _ main__" = = _ name__:for I in ['Flying Rabbit', 'Flying Rabbit Little Sister', 'Flying Rabbit Little Brother']: s.send (I) VIII. Check whether there is a receiver.
If it takes a long time for a sender to prepare before sending a message
In order to avoid wasting performance caused by no receiver
You can first check whether a signal has a receiver.
Send only when it is certain that there is a recipient
From blinker import signala1 = signal ('autofelix-1') a2 = signal (' autofelix-2') def fans (sender): print ('I'm a little fan') a1.connect (fans) if "_ _ main__" = = _ _ name__:res = a1.receiversprint (res) if res:a1.send () res = a2.receiversprint (res) if res:a2.send () else:print ("I'm an idol") IX, whether you subscribe to a signal
Check whether the subscriber is caused by a certain signal
From blinker import signala1 = signal ('autofelix-1') a2 = signal (' autofelix-2') def fans (sender): print ('I'm a little fan') a1.connect (fans) if "_ _ main__" = _ _ name__:res = a1.has_receivers_for (fans) print (res) res = a2.has_receivers_for (fans) print (res). This article "how to create python's blinker signal Library" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the articles, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 285
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.