另一个消息传递库。

bands的Python项目详细描述


BandsLicenseTravis

波段

另一个python消息传递库

有很多python消息传递/信号分发库 在那里。Blinker,PySignal,Dispatch,PyDispatcher,Louie,列表继续 打开。所有这些图书馆都很好。这个图书馆也很好。

功能

  • 已绑定和未绑定的频道
  • 可插拔调度器
  • 波段-具有自己调度器的频道组

使用未绑定通道

>>>importbands>>>defon_alert(message):...returnmessage.upper()>>>alert=bands.channel('alert')>>>alert.connect(on_alert)>>>alert.send('alert!!')['ALERT!!']

或者,您可以通过波段发送信息。发送

>>>bands.send('alert','ALERT!!')['ALERT!!']

send方法将*args**kwargs转发到所有通道的 接受者。

使用绑定通道

当设置了父属性时,通道是bound。如果你使用 bands.channel作为类属性,类的每个实例都有 它是自己绑定的频道。这与python方法的工作方式非常相似, 除了有固定的频道,你不需要得到相同的 每次访问绑定通道实例。

>>>importbands>>>classComponent(object):...started=bands.channel('started')...def__init__(self,name):...self.name=name...self.started.connect(self.on_started)...defon_started(self):...returnself.name+'.on_started'>>>Component.started# doctest:+ELLIPSIS<unboundChannelat0x...>(identifier='started')>>>c1=Component('one')>>>c1.started# doctest:+ELLIPSIS<boundChannelat0x...>(identifier='started')>>>c2=Component('two')>>>c2.started# doctest:+ELLIPSIS<boundChannelat0x...>(identifier='started')>>>c1.started.send()['one.on_started']>>>c2.started.send()['two.on_started']>>>Component.started.send()['one.on_started','two.on_started']>>>bands.send('started')['one.on_started','two.on_started']

从上面的例子中,我们可以看到每个绑定通道都有自己的 订户。另外,如果在未绑定的通道上调用send,则 还将通知信道接收器。我们也可以使用bands.send发送 按标识符显示的消息。

与乐队合作

频带是一组信道,其中的调度器用于实际执行 频道的接收器。发送到一个频带的消息不会到达另一个频带 波段的频道或接收器。

api函数,bands.channelbands.send将调用委托给 活跃的乐队。活动频段默认为默认频段,可通过 默认的带常数。您可以用波段设置活动波段。使用'u band, 并使用波段获取活动波段。有乐队也许是明智的 每个应用程序或库。

>>>importbands>>>my_band=bands.Band()>>>chan=my_band.channel('one')

你也可以为我的乐队提供你自己的调度员。下面是一个 日志调度员。

>>>importbands>>>importlogging>>>classLoggingDispatcher(bands.Dispatcher):...def__init__(self,name):...self.log=logging.getLogger(name)...defbefore_dispatch(self,ctx):...self.log.debug('Sending %s'%ctx.identifier)>>>my_band=bands.Band(LoggingDispatcher('my_band'))

上面的LoggingDispatcher将在每条消息 发送到频道的接收器。

安装

> pip install bands

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

推荐PyPI第三方库


热门话题
java根据两个数组的值对数组进行排序   具有自签名证书和NTLM代理的java Maven SSL repo错误   java自定义字体按钮不工作AndroidStudio   java通过Spring MVC web应用程序向客户端发送文本文件   Java Spring Web服务SOAP身份验证   ANT property environment=“env”无法在JAVA中检索它,但如果作为ANT命令运行,则可以正常工作   java是为spring mvc rest api或spring boot api对应用服务器的每个新请求创建的服务、存储库和组件的新实例吗?   java私有静态最终字符串未完成其工作   PKCS12的安全Java密钥重新处理   java JPA继承表每类SQLSyntaxErrorException