簡體   English   中英

使用 Python Faust 轉發事件

[英]Forwarding Events with Python Faust

我正在嘗試將消息轉發到浮士德的內部主題。 正如本例中 faust 所建議的:

https://faust.readthedocs.io/en/latest/playbooks/vskafka.html

我有以下代碼

in_topic = app.topic("in_topic", internal=False, partitions=1)
batching = app.topic("batching", internal=True, partitions=1)

....

@app.agent(in_topic)
async def process(stream):
    async for event in stream:
        event.forward(batching)
        yield

但是在運行我的 pytest 時總是出現以下錯誤:

 AttributeError: 'str' object has no attribute 'forward'

是否刪除了此功能,或者我是否需要以不同的方式指定主題以獲取事件,或者這甚至是 pytest 的問題?

您正在向后使用語法,這就是為什么!

in_topic = app.topic("in_topic", internal=False, partitions=1)
batching = app.topic("batching", internal=True, partitions=1)

....

@app.agent(in_topic)
async def process(stream):
    async for event in stream:
        batching.send(value=event)
        yield

應該實際工作。

編輯:

這也是我可以正確使用 pytest 的唯一他們。
sink 方法只有在你刪除了 mocked agent 的最后一個 sink 之外的所有東西時才可用,這看起來並不直觀。

如果您首先導入您的接收器,然后將此裝飾器添加到您的測試中,一切都應該正常工作:

from my_path import my_sink, my_agent
from unittest.mock import patch, AsyncMock

@patch(__name__ + '.my_sink.send', new_callable=AsyncMock)
def test_my_agent(test_app)
    async with my_agent.test_context() as agent:
        payload = 'This_works_fine'
        agent.put(payload)
        my_sink.send.assert_called_with(value=payload)

嘗試下一個:

@app.agent(in_topic, sink=[batching])
async def process(stream):
    async for event in stream:
        yield event

@Florian Hall的回答很好。 我還發現 forward 方法僅適用於事件,在我的情況下,我收到了一個 str。 原因可能是浮士德主題和頻道之間的差異。 另一件事是,使用 pytest 和 Faust test_context()的行為很奇怪,例如,即使您沒有定義接收器,它也會迫使您屈服。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM