Conflicting Positional Arguments Using @app.page() Functionality

オープン
#564 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
半日
初心者へのやさしさ
42/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python
領域
backend

調査の方向性

The traceback points to dispatch in faust/web/views.py (around line 118), which calls the handler as method(request, **kwargs), and to how @app.page wraps a plain function as a view method. Start by checking how the wrapped function's signature is bound there. Run a minimal @app.page('/data/') handler that takes only request and returns web.json. Done when the GET returns the JSON body without the takes 1 positional argument but 2 were given TypeError.

索引モデルが issue の本文から書いたものです。

説明

Steps to reproduce

The actual topic, agent, table should not matter. The issue is with the web request. I only included the agent, table, etc in case something about the agent and page decorators conflicted or interacted with each other unexpectedly.

import faust
from faust import web
import pandas as pd
from datetime import timezone
import json

# Faust app setup
app = faust.App('realtime_processor', broker='kafka://localhost:9092')
topic_name = 'first_test_topic'
kafka_topic = app.topic(topic_name, value_type=bytes)
processed_data_table = app.Table('processed_data', default=float, partitions=1)

# A simple model for our items to enforce a schema
class DataItem(faust.Record, validation=True):
    timestamp: str
    value: float

@app.agent(kafka_topic)
async def process(stream):
    async for message in stream:
        data = message
        if 'Items' in data:
            for entry in data['Items']:
                if 'Items' in entry:
                    for item in entry['Items']:
                        # timestamp = pd.to_datetime(item['Timestamp']).replace(tzinfo=timezone.utc).timestamp()
                        timestamp = item['Timestamp']
                        value = item['Value']
                        # Update faust table
                        processed_data_table[timestamp] = value
                        # Print the entire table
                        # print("Current state of the table:")
                        # for key, value in processed_data_table.items():
                        #     print(f"{key}: {value}")
                        # print("-" * 40)  # Just a separator for readability

@app.page('/data/')
async def fetch_first_test_topic(request: web.Request) -> web.Response:
    # Extract query parameters for time range or other filters if needed
    # For example:
    # start_time = request.query['start_time']
    # end_time = request.query['end_time']


    # Convert table data to list of DataItem objects
    # data_list = [DataItem(timestamp=key, value=value).to_representation() 
    #              for key, value in processed_data_table.items()]

    # # Convert list of DataItem objects to JSON
    # json_data = json.dumps(data_list)
    json_data={'hello':'world'}
    
    # Return JSON response
    return web.json(json_data)

Script I used to reach this endpoint:

import pandas as pd
import requests

response = requests.get('http://127.0.0.1:6066/data/')
print(response.text)
data = response.json()  # This should be the JSON array of objects
print(data)

Expected behavior

I was expecting from the print statement of the get request to be

{'hello':'world'}

Actual behavior

I got errors on both the get request side and the faust side, suggesting the that get request communicated with the faust app, but for some reason there is an error with positional arguments, despite none being defined.

Full traceback

On the faust app side

[2023-11-08 16:23:29,783] [2282037] [ERROR] Error handling request 
Traceback (most recent call last):
  File "/home/ksuden/anaconda3/envs/py311/lib/python3.11/site-packages/aiohttp/web_protocol.py", line 433, in _handle_request
    resp = await request_handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ksuden/anaconda3/envs/py311/lib/python3.11/site-packages/aiohttp/web_app.py", line 504, in _handle
    resp = await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ksuden/anaconda3/envs/py311/lib/python3.11/site-packages/faust/web/drivers/aiohttp.py", line 246, in _dispatch
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ksuden/anaconda3/envs/py311/lib/python3.11/site-packages/faust/web/views.py", line 102, in __call__
    return await self.dispatch(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ksuden/anaconda3/envs/py311/lib/python3.11/site-packages/faust/web/views.py", line 118, in dispatch
    response = await method(cast(Request, request), **kwargs)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: fetch_first_test_topic() takes 1 positional argument but 2 were given

On the GET request side

500 Internal Server Error

Server got itself in trouble
Traceback (most recent call last):
  File "/home/ksuden/anaconda3/envs/py311/lib/python3.11/site-packages/requests/models.py", line 971, in json
    return complexjson.loads(self.text, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ksuden/anaconda3/envs/py311/lib/python3.11/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ksuden/anaconda3/envs/py311/lib/python3.11/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 5 (char 4)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/ksuden/source/Tps-PricePrediction/data_works/data_streaming/faust/api_query.py", line 6, in <module>
    data = response.json()  # This should be the JSON array of objects
           ^^^^^^^^^^^^^^^
  File "/home/ksuden/anaconda3/envs/py311/lib/python3.11/site-packages/requests/models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Extra data: line 1 column 5 (char 4)

Versions

  • Python version: 3.11
  • Faust version: 0.10.16
  • Operating system: Linux
  • Kafka version: 2.0.2
  • RocksDB version (if applicable)
主要言語
Python
スター
1.9k
フォーク
205
平均マージ
2時間 2分
マージ済み PR(30日)
1

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

faust-streaming/faust のほかの issue

faust-streaming/faust の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。