msgpack errors when using iter() with intervals between each batch call
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
調査の方向性
scrapinghub/client/proxy.py、特に iter と、scrapinghub/hubstorage/serialization.py、特に mpdecode から始めます。チャンクサイズ 25000 と 50000 で報告された失敗を再現し、その後、動作するサイズ 1000 と比較します。大きなチャンクを処理する際に、報告された UnicodeDecodeError や unhashable-dict の失敗が発生せず、反復処理の信頼性が維持されれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Good Day!
I've encountered this peculiar issue when trying to save up memory by processing the items in chunks. Here's a strip down version of the code for reproduction of the issue:
import pandas as pd
from scrapinghub import ScrapinghubClient
def read_job_items_by_chunk(jobkey, chunk=10000):
"""In order to prevent OOM issues, the jobs' data must be read in
chunks.
This will return a generator of pandas DataFrames.
"""
client = ScrapinghubClient("APIKEY123")
item_generator = client.get_job(jobkey).items.iter()
while item_generator:
yield pd.DataFrame(
[next(item_generator) for _ in range(chunk)]
)
for df_chunk in read_job_items_by_chunk('123/123/123'):
# having a small chunk-size like 10000 won't have any problems
for df_chunk in read_job_items_by_chunk('123/123/123', chunk=25000):
# having a bug chunk-size like 25000 will throw out errors like the one below
Here's the common error it throws:
<omitted stack trace above>
[next(item_generator) for _ in range(chunk)]
File "/usr/local/lib/python2.7/site-packages/scrapinghub/client/proxy.py", line 115, in iter
_path, requests_params, **apiparams
File "/usr/local/lib/python2.7/site-packages/scrapinghub/hubstorage/serialization.py", line 33, in mpdecode
for obj in unpacker:
File "msgpack/_unpacker.pyx", line 459, in msgpack._unpacker.Unpacker.__next__ (msgpack/_unpacker.cpp:459)
File "msgpack/_unpacker.pyx", line 390, in msgpack._unpacker.Unpacker._unpack (msgpack/_unpacker.cpp:390)
File "/usr/local/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x8b in position 67: invalid start byte
Moreover, it throws out a different error when using a much bigger chunk-size, like 50000:
<omitted stack trace above>
[next(item_generator) for _ in range(chunk)]
File "/usr/local/lib/python2.7/site-packages/scrapinghub/client/proxy.py", line 115, in iter
_path, requests_params, **apiparams
File "/usr/local/lib/python2.7/site-packages/scrapinghub/hubstorage/serialization.py", line 33, in mpdecode
for obj in unpacker:
File "msgpack/_unpacker.pyx", line 459, in msgpack._unpacker.Unpacker.__next__ (msgpack/_unpacker.cpp:459)
File "msgpack/_unpacker.pyx", line 390, in msgpack._unpacker.Unpacker._unpack (msgpack/_unpacker.cpp:390)
TypeError: unhashable type: 'dict'
I find that the workaround/solution for this would be to have a lower value for chunk. So far, 1000 works great.
This uses scrapy:1.5 stack in Scrapy Cloud.
I'm guessing this might have something to do with the long waiting time that happens when processing the pandas DataFrame chunk, and when the next batch of items are being iterated, the server might have deallocated the pointer to it or something.
May I ask if there might be a solution for this? since a much bigger chunk size will help with the speed of our jobs.
I've marked it as bug for now as this is quite an unexpected/undocumented behavior.
Cheers!
- 主要言語
- Python
- スター
- 204
- フォーク
- 62
- 平均マージ
- 6分
- マージ済み PR(30日)
- 2
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
scrapinghub/python-scrapinghub のほかの issue
-
Incorrect information for Samples in Job documentation対応中かも @AdrianAtZyte が 66 日前に担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
-
Zyte Job API: Provision to support Cron Expression or Time Interval対応中かも @AdrianAtZyte が 67 日前に担当しました。 オープン
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
-
Add async support再び着手できるかも @VMRuiz が 1414 日前に担当しましたが、オープン中のプルリクエストはありません。 オープン
scrapinghub/python-scrapinghub#169 · コメント 8 件 · リアクション 2 件 · 担当者 4 名 ·
-
Add retry logic to Job Tag Update function対応中かも @AdrianAtZyte が 66 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 42/100
-
KeyError: 'status' when trying to schedule spider対応中かも @AdrianAtZyte が 64 日前に担当しました。 オープンenhancement
難易度 3/5 1〜2日 初心者へのやさしさ 30/100
scrapinghub/python-scrapinghub#156 · コメント 6 件 ·
scrapinghub/python-scrapinghub の issue をすべて見る
似ている issue
-
[19.0] pos_sale: settling an order with a 0 down payment line crashes (reading 'reward_id') and leaves a wrong amount due対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 79/100
developmentseed/titiler-covjson#164 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
awslabs/visual-asset-management-system#2164 ·
メンテナーはふだん 1 日以内に返信
-
needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
MakazhanAlpamys/Soup#3589 ·
メンテナーはふだん 1 日以内に返信
-
channels status reports the wrong Enabled value for Feishu instances and default-enabled WebSocketオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
メンテナーはふだん 1 日以内に返信