gpt4 book ai didi

python-3.x - 如何从 faust 应用程序向 Websocket 发送数据

转载 作者:行者123 更新时间:2023-12-05 08:52:58 26 4
gpt4 key购买 nike

我目前正在研究一个用例,使用 Kafka 和 robinhood 的 faust 来处理来自 Kafka 的数据。我已经成功地进行了计算,我需要的结果正在打印到我的 faust worker 正在运行的控制台上。

现在我想找到一种方法,使我的结果不仅在控制台中而且在 HTML 页面中可见。我查看了 websockets 库,但无法将其与 faust 结合使用。我得到的错误是 Crashed reason=RuntimeError('This event loop is already running') 我认为这是因为代码是为每条正在处理的消息执行的。

非常感谢任何帮助

这是我使用的代码:

    import faust, datetime, websockets, asyncio

app = faust.App(
'UseCase',
broker='kafka://localhost:29092',
)

usecase_topic = app.topic('usecase',partitions=8)

usecase_table = app.Table('usecase', default=int)

checkfailure = {}

@app.agent(usecase_topic)
async def process_record(records):
async for record in records:
#count records for each Sensor
print(record)
sensor = record['ext_id']
usecase_table[sensor] += 1
#print(f'Records for Sensor {sensor}: {usecase_table[sensor]}')

#write current timestamp of record and previous timestamp for each sensor to usecase_table dict
currtime_id = record['ext_id']+'c'
prevtime_id = record['ext_id']+'p'
usecase_table[currtime_id] = datetime.datetime.strptime(record['tag_tsp'], "%Y%m%d%H%M%S.%f")

#print current time
print(f'Current time for Sensor {sensor}: {usecase_table[currtime_id]}')


#calculate and print timestamp delta; if no previous value is given print message
if usecase_table[prevtime_id] == 0:
print(f'no previous timestamp for sensor {sensor}')
else:
usecase_table[prevtime_id] = datetime.datetime.strptime(usecase_table[prevtime_id], "%Y%m%d%H%M%S.%f")
print(f'previous time for Sensor {sensor}: {usecase_table[prevtime_id]}')
tsdelta = usecase_table[currtime_id] - usecase_table[prevtime_id]
tsdelta_id = record['ext_id']+'t'
usecase_table[tsdelta_id] = str(tsdelta)
print(f'Sensor: {sensor} timestamp delta: {usecase_table[tsdelta_id]}')

#calculate value delta
currvalue_id = record['ext_id']+'cv'
prevvalue_id = record['ext_id']+'pv'
usecase_table[currvalue_id] = record['tag_value_int']

print(f'current value for Sensor {sensor}: {usecase_table[currvalue_id]}')

if usecase_table[prevvalue_id] == 0:
print(f'no previous record for sensor {sensor}')
else:
print(f'previous value for Sensor {sensor}: {usecase_table[prevvalue_id]}')
vdelta = usecase_table[currvalue_id] - usecase_table[prevvalue_id]
vdelta_id = record['ext_id']+'v'
usecase_table[vdelta_id] = vdelta
print(f'Sensor: {sensor} value delta:{usecase_table[vdelta_id]}')

#calculate cycle time
if usecase_table[prevtime_id] != 0 and usecase_table[prevvalue_id] != 0 and usecase_table[vdelta_id] != 0:
cycletime = tsdelta / usecase_table[vdelta_id]
cyclemsg = f'Sensor {sensor}; Cycletime {cycletime}'
print(cyclemsg)

#add timestamp to checkfailure dict
checkfailure[sensor] = datetime.datetime.strptime(record['tag_tsp'], "%Y%m%d%H%M%S.%f")
#check if newest timestamp for a sensor is older than 10 secs
for key in checkfailure:
if datetime.datetime.now() - checkfailure[key] >= datetime.timedelta(seconds=10):
failuremsg = f'Error: Sensor {key}'
print(failuremsg)

#send results to websocket
async def send_result(websocket,path):
results = cyclemsg + failuremsg
await websockets.send(results)
start_server = websockets.serve(send_result, '127.0.0.1', 5678)
asyncio.get_event_loop().run_until_complete(start_server)

#set previous value and timestamp to current
usecase_table[prevtime_id] = record['tag_tsp']
usecase_table[prevvalue_id] = record['tag_value_int']

最佳答案

被这个 asyncio 错误消息弄糊涂是正常的:)

您不能从 async def 函数调用 loop.run_until_complete

您需要做的是在后台启动 websocket 服务器。这应该很简单,它使用的是 asyncio.ensure_future,但您还希望 websocket 服务器在应用程序退出时正常关闭。

为此 Faust 使用“服务”,您可以为您的 websocket 服务器定义一个服务:

import faust
import websockets
from mode import Service
from websockets.exceptions import ConnectionClosed
from websockets.server import WebSocketServerProtocol


class App(faust.App):

def on_init(self):
self.websockets = Websockets(self)

async def on_start(self):
await self.add_runtime_dependency(self.websockets)



class Websockets(Service):

def __init__(self, app, bind: str = 'localhost', port: int = 9999, **kwargs):
self.app = app
self.bind = bind
self.port = port
super().__init__(**kwargs)

async def on_message(self, ws, message):
...

async def on_messages(self,
ws: WebSocketServerProtocol,
path: str) -> None:
try:
async for message in ws:
await self.on_message(ws, message)
except ConnectionClosed:
await self.on_close(ws)
except asyncio.CancelledError:
pass

async def on_close(self, ws):
# called when websocket socket is closed.
...

@Service.task
def _background_server(self):
await websockets.serve(self.on_messages, self.bind, self.port)

app = App('UseCase')
# [...]

关于python-3.x - 如何从 faust 应用程序向 Websocket 发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54934058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com