运行以下代码时(理论上它应该每分钟发送一次值)
from __future__ import print_function
from twisted.internet.ssl import CertificateOptions
options = CertificateOptions()
from os import environ
from twisted.internet.defer import inlineCallbacks
from twisted.internet import reactor
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner
from autobahn import wamp
from datetime import datetime, timedelta
import xlwings as wb
import time
import xlwings as wb
class Component(ApplicationSession):
"""
An application component that publishes an event every second.
"""
@inlineCallbacks
def onJoin(self, details):
print("session attached")
while True:
try:
wb.Book(r'C:\Users\Administrator\Desktop\Datasets\test_feed.xlsx')
e = wb.Range('A2').value
b = wb.Range('C2').value
c = wb.Range('E2').value
except Exception:
print("----Waiting for RTD server response----")
time.sleep(1)
try:
epoch = datetime(now.year, now.month, now.day)
result = epoch + timedelta(days=c)
result = result.replace(microsecond=0, second=0)
if result > now:
now = result
print("Stock", e, "Time", now, "Price", b)
self.publish(u'com.myapp.ma', b)
except Exception:
print("-----Waiting1 for RTD server response----")
time.sleep(1)
def onDisconnect(self):
print("disconnected")
reactor.stop()
if __name__ == '__main__':
runner = ApplicationRunner(
environ.get("AUTOBAHN_DEMO_ROUTER", u"ws://127.0.0.1:8080/ws"),
u"crossbardemo")
runner.run(Component)
返回以下错误
2017-12-28T18:43:52+0100 [Router 1604] dropping connection to peer tcp4:127.0.0.1:61531 with abort=True: WebSocket ping timeout (peer did not respond with pong in time)
2017-12-28T18:43:52+0100 [Router 1604] session "8526139172223346" left realm "crossbardemo"
我试图解决这个问题:
I)
from twisted.internet.ssl import CertificateOptions options = CertificateOptions() if __name__ == '__main__': runner = ApplicationRunner( environ.get("AUTOBAHN_DEMO_ROUTER", u"ws://127.0.0.1:8080/ws"), u"crossbardemo", ssl=options) runner.run(Component)
II)
if __name__ == '__main__': runner = ApplicationRunner( environ.get("AUTOBAHN_DEMO_ROUTER", u"ws://127.0.0.1:8080/ws"), u"crossbardemo", ) runner.run(Component, auto_reconnect=True)
III)
注册表编辑
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS
1.0]
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS
1.0 \客户端] " DisabledByDefault" = DWORD:00000000 "启用" = DWORD:00000001
IV)
安装certifi模块(pip install certifi)设置SSL_CERT_FILE,就像 导出SSL_CERT_FILE =" $(python -m certifi)"
仍有相同的错误。我在Windows 10上运行,带有crossbar演示路由器,高速公路和扭曲。
链接到路由器配置:
此外,以下示例代码正常运行:
counter = 100
while True:
print("publish: com.myapp.ma", counter)
self.publish(u'com.myapp.ma', counter)
counter += 100
yield sleep(30)
答案 0 :(得分:1)
对于Twisted来处理进一步的I / O事件,您必须将控制权交还给反应堆。 Twisted实现了一个协作式多任务系统。各种任务在反应器线程中运行。这是通过每个任务仅花费一小段时间来控制来实现的。代码如:
while True:
...
sleep(1)
阻止任何其他任务执行控制,并阻止反应堆获得对服务I / O事件的控制。
由于此代码位于使用inlineCallbacks
修饰的函数内,因此会有一个非常小的更改,使其至少与Twisted的操作模式不完全不兼容。
而不是time.sleep(1)
,请尝试以下表达式:
yield deferLater(reactor, 1, lambda: None)
从deferLater
导入twisted.internet.task
。这将执行" sleep"它将控制权交还给反应堆,并让其他任务在睡眠期间执行。这应该允许Autobahn发送必要的ping / pong消息,并允许它处理您的publish
电话。