简体   繁体   English

使用 python-asyncio,我如何读取 url 而不是在主 function 中列出 url?

[英]Using python-asyncio, how can I read the urls instead of listing the urls inside the main function?

I have a list of urls saved inside a text file called file.txt.我有一个保存在名为 file.txt 的文本文件中的 url 列表。 How can I have this script read from the txt file and save the response to a text file instead of listing the urls inside the function?如何从 txt 文件中读取此脚本并将响应保存到文本文件,而不是在 function 中列出 url? There are too many urls to list so the function will get complicated to look at.要列出的 URL 太多,因此 function 看起来会很复杂。 Thank you谢谢

import asyncio
import aiohttp
from codetiming import Timer

async def task(name, work_queue):
    timer = Timer(text=f"Task {name} elapsed time: {{:.1f}}")
    async with aiohttp.ClientSession() as session:
        while not work_queue.empty():
            url = await work_queue.get()
            print(f"Task {name} getting URL: {url}")
            timer.start()
            async with session.get(url) as response:
                await response.text()
            timer.stop()

async def main():
    """
    This is the main entry point for the program
    """
    # Create the queue of work
    work_queue = asyncio.Queue()

    # Put some work in the queue
    for url in [
        "http://google.com",
        "http://yahoo.com",
        "http://linkedin.com",
        "http://apple.com",
        "http://microsoft.com",
        "http://facebook.com",
        "http://twitter.com",
    ]:
        await work_queue.put(url)

    # Run the tasks
    with Timer(text="\nTotal elapsed time: {:.1f}"):
        await asyncio.gather(
            asyncio.create_task(task("One", work_queue)),
            asyncio.create_task(task("Two", work_queue)),
        )

if __name__ == "__main__":
    asyncio.run(main())

Text.file contents文本文件内容

http://google.com
http://yahoo.com
http://linkedin.com
http://apple.com
http://microsoft.com
http://facebook.com

I'm guessing you don't need to load the urls asynchronously.我猜你不需要异步加载网址。 You can load the urls before running the main function.您可以在运行main function 之前加载 url。 Define the main function so it accepts the parameter urls and finally loop through the urls.定义main function 以便它接受参数urls并最终遍历 urls。

async def main(urls):
  queue = asyncio.Queue()
  for url in urls:
    await queue.put(url)
  
  # rest of your code


if __name__ == "__main__":
  with open("urls.txt") as infile:
    data = infile.read()
    urls = data.split("\n")[:-1]
  asyncio.run(main(urls))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用python-asyncio时如何重载模块? - How to overload modules when using python-asyncio? 从文本文件中读取 url,而不是在 function 中列出一个 url? - Reading urls from a text file instead of listing one url inside the function? 如何在python3.7中测量python-asyncio - How to measure python-asyncio in python3.7 Python-asyncio - 使用计时器/线程终止异步 - Python-asyncio - using timers/threading to terminate async 使用Python 3进行抓取时,如何一次将功能同时启动到5个网址 - How can I start function at same time to 5 urls at once while scraping using Python 3 如何使用 url 访问 Python 中的 s3 文件? - How can I access s3 files in Python using urls? 如何在主异步循环内的同步子进程中运行多个异步循环? - How can I run multiple asyncio loops inside syncrhonous sub-processes inside a main asyncio loop? python-asyncio:走上协程链 - python-asyncio: walk up the coroutine chain 如何基于 python-asyncio 的传输和协议 api 实现类似 sock.recv() 的逻辑? - How to implement logic like sock.recv(), based on python-asyncio's transport & protocol api? python-asyncio TypeError:对象字典不能用于“等待”表达式 - python-asyncio TypeError: object dict can't be used in 'await' expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM