Py学习  »  Python

从csv列表加速Python请求

Dave_p • 4 年前 • 569 次点击  

以下是我第一次尝试使用Python。我有一个csv文件,上面列出了客户的工作编号。我正在对我们的(第三方)工作管理系统进行3次api调用。1获取实际作业编号,2获取作业阶段,3获取作业状态(如下)。我有一个vb.net应用程序,可以将这些结果分离开来,将它们与其他数据混合,并将它们添加到MYSQL数据中,以便在Ruby仪表板上进行报告等。API调用的繁琐工作是在excel vb a中完成的,因为我可以在一个地方完成所有工作。API调用很慢,非常慢,所以想办法加快速度,Python就出现了。排序如下,并从.net调用它,但仍然很慢。这三个电话总共需要5分钟。我们只看到大约60到70个工作岗位。对作业号的第一次调用确实带来了一个大的json文件,但不幸的是,第二次和第三次调用中没有我需要的数据,因此我无法通过消除2和3来加快它的速度。

由于我刚跳入Python head,我无法轻松找到解决方案。我一直在看多线程,多处理的帖子,但都是问谁有半个线索,我只是还没有到那里。

非常感谢您的帮助。

import requests
import csv


with open("C:\\******.csv") as csvfile:
    readCSV = csv.reader(csvfile, delimiter=",")
    jobs = []
    Stages = []
    outfile = open("C:\\*********another****.csv", "w")
    for row in readCSV:
        job = row[0]
        url = (
            "https:/********.com/api/v1.0/companies/**/jobs/"
            + job
            + "?columns=Status"
        )
        auth_token = "*******"

        payload = ""
        headers = {
            "Content-Type": "application/json x-www-form-urlencoded",
            "Authorization": "Bearer *********",
            "Accept": "*/*",
            "Cache-Control": "no-cache",
            "Host": "*****",
            "Accept-Encoding": "gzip, deflate",
            "Connection": "keep-alive",
            "cache-control": "no-cache",
        }

        response = requests.request(
            "GET", url, data=payload, headers=headers
        )
        Stage = response.text

        print((job, Stage), file=outfile)

    outfile.close
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50694
 
569 次点击  
文章 [ 2 ]  |  最新文章 4 年前
Valery Ramusik
Reply   •   1 楼
Valery Ramusik    4 年前

多处理取决于核心数。在你的例子中,线程是你所需要的,线程最简单的接口是ThreadPool。例如。:

from multiprocessing.pool import ThreadPool

threads_count = 10
pool = ThreadPool(threads_count)

def do_job(url)
    return requests.get(url)

urls = ['url1', 'url2']

pool.map(do_job, urls)

pool.close()
pool.join()
AKX
Reply   •   2 楼
AKX    4 年前

像这样的事情应该能让你得到多个处理的工作。

其思想是将作业从CSV重构为一个生成器函数,然后将访问API的函数重构为另一个生成器函数。

(因为你显然是在窗户上,所以你需要使用 main() 函数和 if main 守卫 multiprocessing .)

import requests
import csv
import multiprocessing

sess = requests.Session()

headers = {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Authorization": "Bearer *********",
    "Cache-Control": "no-cache",
    "Host": "*****",
}


def read_jobs():
    with open("C:\\******.csv") as csvfile:
        for row in csv.reader(csvfile, delimiter=","):
            yield row[0]


def get_job_status(job):
    url = (
        "https:/********.com/api/v1.0/companies/**/jobs/"
        + job
        + "?columns=Status"
    )
    response = requests.request(
        method="GET", url=url, headers=headers
    )
    print("Got job ", job)
    return (job, response.text)


def main():
    with open("C:\\*********another****.csv", "w") as outfile:
        with multiprocessing.Pool() as pool:
            for result in pool.imap_unordered(
                get_job_status, read_jobs()
            ):
                print(result, file=outfile)


if __name__ == "__main__":
    main()