日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python异步写文件_结合异步http请求将数据写入文件

發(fā)布時(shí)間:2025/4/5 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python异步写文件_结合异步http请求将数据写入文件 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我從here編輯了此代碼:import asyncio

import time

from aiohttp import ClientPayloadError

from aiohttp import ClientSession

COUNTER = 1

async def fetch(url, session):

async with session.get(url) as response:

delay = response.headers.get("DELAY")

date = response.headers.get("DATE")

global COUNTER

COUNTER +=1

print("{}. {}:{} with delay {}".format(str(COUNTER), date, response.url, delay))

try:

return await response.text()

except ClientPayloadError:

print("ERROR: ".format(url))

async def bound_fetch(sem, url, session):

# Getter function with semaphore.

async with sem:

await fetch(url, session)

async def run():

urls = [build_url(id) for id in load_ids()]

tasks = []

# create instance of Semaphore

sem = asyncio.Semaphore(1000)

# Create client session that will ensure we dont open new connection

# per each request.

async with ClientSession(conn_timeout=10000, read_timeout=10000) as session:

for url in urls:

#pass Semaphore and session to every GET request

task = asyncio.ensure_future(bound_fetch(sem, url, session))

tasks.append(task)

responses = asyncio.gather(*tasks)

await responses

def build_url(id):

url = 'http://api.metagenomics.anl.gov/annotation/sequence/{}?source=Refseq'.format(id)

return url

def load_ids():

#in the "real" code I will load a file here and extract the ids

return """

mgm4558908.3

mgm4484962.3

mgm4734169.3

mgm4558911.3

mgm4484983.3

mgm4558918.3

""".strip().split()

start = time.clock()

loop = asyncio.get_event_loop()

future = asyncio.ensure_future(run())

loop.run_until_complete(future)

run_time = (start - time.clock())/60

print("this took: {} minutes".format(run_time))

我知道我可以使用:print(await response.text())打印響應(yīng)數(shù)據(jù),但是我不喜歡異步代碼,因此我不知道應(yīng)該如何以及在哪里打開(kāi)并寫(xiě)入文件。因?yàn)槲艺J(rèn)為在同一時(shí)間寫(xiě)入同一個(gè)文件時(shí),有某種線程可能會(huì)導(dǎo)致問(wèn)題(我熟悉多處理)。在

總結(jié)

以上是生活随笔為你收集整理的python异步写文件_结合异步http请求将数据写入文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。