【问题标题】:How to implement multithreading or multiprocessing of a request如何实现请求的多线程或多处理
【发布时间】:2020-04-15 12:39:45
【问题描述】:

我希望实现request 对象的多线程或多处理。

我的代码如下:

def validate(testurl):
    json_d = {"task_id": "user_uid","data": {"document1":testurl}} 
    response = requests.post("https://example.net.com/document",headers=headers,json=json_d)
    my_data1 = response.text
    with open("testurl.txt","a+") as file:
        file.write(my_data1)
    my_data = json.loads(my_data1)
    result = {'bool_value':my_data['data']}
    return result

有没有办法对超过 5000 个 URL 的 Pandas apply() 函数进行多线程或多处理?例如:

df['res'] = df['testurl'].apply(validate)

我应该在下面使用这个吗?

from joblib import parallel, delayed

【问题讨论】:

  • 你试过threading模块吗?

标签: python pandas multithreading multiprocessing


【解决方案1】:

您可以使用 swifter 或 dask 来执行此操作。可以参考https://gdcoder.com/speed-up-pandas-apply-function-using-dask-or-swifter-tutorial/

df['res'] = df['testurl'].swifter.apply(lambda x: validate(x))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多