[英]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.