支持异步的信号和插槽实现

Signaller的Python项目详细描述


支持异步的信号和插槽实现

时隙可以是函数、方法或协同程序。默认情况下使用弱引用。 如果slot是coroutine,它将被调度为与^{tt1}异步运行$ (但您必须自己运行事件循环)。

还可以通过指定force_async=Truewhen异步运行阻塞函数 将信号连接到插槽(它仅适用于该插槽)或在创建信号时(它将 适用于所有连接的插槽)。默认使用具有5个工作线程的threadpoolexecutor, 但是当使用executor参数创建信号时,可以对其进行更改。

要求

  • python=3.4

用法

示例:

importloggingfromsignallerimportSignal,autoconnect# Enable verbose logginglogging.basicConfig(level=logging.DEBUG)# Creating signals (you can set signal name, but it is not required,# signals can be anonymous):sig_test=Signal('sig_test')# Connecting signals to slots (uses weak references by default,# but you can force strong references by specifying weak=False):defslot(arg):print('slot:',arg)sig_test.connect(slot)sig_test.connect(lambdaarg:print('slot_lambda:',arg),weak=False)# You can also use decorators for connecting signals to slots:@sig_test.connectdefslot2(arg):print('slot2:',arg)# And keyword arguments can be specified when using decorators too:@sig_test.connect(force_async=True)defslot3(arg):print('slot3:',arg)# You can also use decorators on methods, then signals will be connected to instance# methods automatically whenever new instance is created. But you must decorate class# with @autoconnect decorator for autoconnection to work. Class methods and# static methods are not supported.@autoconnectclassCls:@sig_test.connectdefslot4(self,arg):print('slot4:',arg)obj=Cls()# Slots are automatically disconnected from signals# when using weak references:delslot# Or you can disconnect slots manually:sig_test.disconnect(slot2)# Emitting signals (you can send both positional and keyword# arguments to connected slots):sig_test.emit('Hello world!')

输出:

INFO:signaller:Connecting signal <Signal 'sig_test' at 0x7f3c468bfc50> to slot <function slot at 0x7f3c46cc6f28>
INFO:signaller:Connecting signal <Signal 'sig_test' at 0x7f3c468bfc50> to slot <function <lambda> at 0x7f3c468c97b8>
INFO:signaller:Connecting signal <Signal 'sig_test' at 0x7f3c468bfc50> to slot <function slot2 at 0x7f3c43c9e400>
INFO:signaller:Connecting signal <Signal 'sig_test' at 0x7f3c468bfc50> to slot <function slot3 at 0x7f3c43c9e598>
DEBUG:signaller:Marking instance method <function Cls.slot4 at 0x7f3c43c9e6a8> for autoconnect to signal <Signal 'sig_test' at 0x7f3c468bfc50>
INFO:signaller:Connecting signal <Signal 'sig_test' at 0x7f3c468bfc50> to slot <bound method Cls.slot4 of <__main__.Cls object at 0x7f3c43f11d30>>
DEBUG:signaller:Object <function slot at 0x7f3c46cc6f28> has been deleted
INFO:signaller:Disconnecting slot <Reference (weak) to <function slot at 0x7f3c46cc6f28> (dead)> from signal <Signal 'sig_test' at 0x7f3c468bfc50>
INFO:signaller:Disconnecting slot <function slot2 at 0x7f3c43c9e400> from signal <Signal 'sig_test' at 0x7f3c468bfc50>
INFO:signaller:Emitting signal <Signal 'sig_test' at 0x7f3c468bfc50>
DEBUG:signaller:Calling slot <Reference (weak) to <function slot3 at 0x7f3c43c9e598>> asynchronously (in executor <concurrent.futures.thread.ThreadPoolExecutor object at 0x7f3c468bff28>)
slot3: Hello world!
DEBUG:signaller:Calling slot <Reference (strong) to <function <lambda> at 0x7f3c468c97b8>>
slot_lambda: Hello world!
DEBUG:signaller:Calling slot <Reference (weak) to <bound method Cls.slot4 of <__main__.Cls object at 0x7f3c43f11d30>>>
slot4: Hello world!

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java置换相关算法   在Java中读取/打开文本文件   java为什么这段代码不适用于CodeEval?   java如何将日历转换为JSON   从txt文件中读取字符串并将其存储到java中的字符数组中   字符编码Java ResourceBundles umlauts搞砸了   java为什么hashmap会根据总大小而不是填充的存储桶调整大小   java如何将Excel单元格中的数字字符串读取为字符串(而不是数字)?   java Guava的LocalCache无法使用,为什么?   java有没有办法强制JVM在单个处理器或内核上运行   java Eclipse不安装软件   将字节转换为java字符串(可能是汉字)   Java正则表达式:提取函数名   JavaTestNG:如何从多个类中指定测试方法顺序?