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

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更新的列表也不会显示在jsp页面中   java Apache:无法启动上下文路径/网站上的失败应用程序   java验证CSV中的特定列   对于具有专用内存的java应用程序,最小堆大小低于最大堆大小有意义吗?   java将数组中的值转换为多维数组   java在给定程序中,垃圾收集器在对象被取消引用之前正在运行。。。使用jre 7(32位)   java在运行时动态刷新文件夹   eclipse如何解决“java.net.BindException:地址已在使用:JVM_Bind”错误?   Java数组与数组   每次任务完成任务时,Java多线程都会安排任务   java部分编译时使用maven编织第三方jar   java Dokku单一回购中的多个应用程序   用apachevelocity生成javac/C++语言文件   java如何使用spring应用程序上下文中的属性文件实例化列表   java访问智能卡文件结构   具有GlobalMethodSecurity的java自定义UserDetailService循环引用   java如何集成Spring和JSF