No project description provided
Project description
说明
本包用于使用多线程调接口保存结果
输入一个df格式的原始数据,用每一行生成请求参数,来调用某个接口,分批次保存结果到文件。 结果文件支持fth, csv, xlsx三种格式。
示例
1. 准备数据和函数
# a. 输入数据 df (DataFrame格式)
## df的每一行是一次请求需要的原始数据
# b. 处理单次输入的函数
import json
def makeReqData(json_str):
# json_str: row.to_json() df的一行数据
# TODO: 使用json_str生成请求参数
json_str = json.loads(json_str)
return json_str
# c. 处理单次输出的函数
def makeResult(r):
# r: res.json(), 接口返回的json
# TODO: 选取需要保存的字段, 保存为新的dict, 用于写到文件
data = r.get("data")
return data
2. 多线程调用接口保存文件
# 方法一
from multi_request import mreq
m = mreq.MultiRequest()
m.url = "http://127.0.0.1:8080/hello" # 请求接口, 目前只支持 POST 方法
m.makeReqData = makeReqData # 你的生成单次请求数据的函数
m.makeResult = makeResult # (可选) 处理单次返回数据的函数, 生成最终结果字典
m.input_data = df # 原始请求数据, pandas的 DataFrame 格式
m.parallel_batch_size = 20 # (可选) 并发数,默认: 100
m.save_batch_size = 12 # (可选) 每几个保存一个文件,默认: 5000
m.res_format = "fth" # (可选) 默认: fth, 支持格式: fth, csv, xlsx
m.res_dir = "data" # (可选) 保存结果的目录, 默认: ./data
m.run()
# 方法二
from multi_request import mreq
params = {
"url": "http://127.0.0.1:8080/hello",
"makeReqData": makeReqData,
"makeResult": makeResult,
"input_data": df,
"parallel_batch_size": 20,
"save_batch_size": 8,
"res_format": "csv",
"res_dir": "tmp_csv",
}
m = mreq.MultiRequest(**params)
m.run()
# 使用默认参数
from multi_request import mreq
params = {
"url": "http://127.0.0.1:8080/hello",
"makeReqData": makeReqData,
"input_data": df,
}
m = mreq.MultiRequest(**params)
m.run()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
multi_request-0.0.7.tar.gz
(3.3 kB
view details)
Built Distribution
File details
Details for the file multi_request-0.0.7.tar.gz
.
File metadata
- Download URL: multi_request-0.0.7.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7838eed6cf53dd184512b3d4234c698e5faf78b1f5f912cb7e37ed943c4bf613 |
|
MD5 | 7c146490273a0bdb14e275ddcea746bd |
|
BLAKE2b-256 | e07d5d6c8ec4de2e4af30d451e4502ad8007d0d0bd300abd9b473e76f71cb556 |
File details
Details for the file multi_request-0.0.7-py3-none-any.whl
.
File metadata
- Download URL: multi_request-0.0.7-py3-none-any.whl
- Upload date:
- Size: 3.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb3780e3542d455569faff2e1c9dc2456d1d51d5a85021a5688d6a76cc124b4f |
|
MD5 | d240a758703511092a1aa7fc5f783f78 |
|
BLAKE2b-256 | 3d210932a4d9e38dfaec775fbaf056e1082a95e1ea7d6628919f39991fb84fcd |