
[英]CLIENT PROGRAM ERROR An operation was attempted on something that is not a socket
[英]Socket error (An operation was attempted on something that is not a socket) on aiohttp function
async def simultaneous_chunked_download(urls_paths, label):
timeout = ClientTimeout(total=60000)
sem = asyncio.Semaphore(5)
async with aiohttp.ClientSession(timeout=timeout, connector=aiohttp.TCPConnector(verify_ssl=False)) as cs:
async def _fetch(r, path):
async with sem:
async with aiofiles.open(path, "wb") as f:
async for chunk in r.content.iter_any():
if not chunk:
break
size = await f.write(chunk)
if not indeterminate:
bar._done += size
bar.show(bar._done)
if indeterminate:
bar._done += 1
bar.show(bar._done)
indeterminate = False
total_length = 0
tasks = []
for url, path in urls_paths.items():
r = await cs.get(url)
if not indeterminate:
try:
total_length += r.content_length
except Exception:
indeterminate = True
tasks.append(_fetch(r, path))
verbose_print(f"url: {url},\npath: {path}\n\n")
if not indeterminate:
bar = progress.Bar(
expected_size=total_length, label=label, width=28, hide=False
)
else:
bar = progress.Bar(
expected_size=len(tasks), label=label, width=28, hide=False
)
logger._pause_file_output = True
bar.show(0)
bar._done = 0
await asyncio.gather(*tasks)
logger._pause_file_output = False
bar.done()
我上面的 function 用于异步下载 url 字典,然后打印出进度条。 其用法示例:
代码本身运行得很好,但是我不断收到这些错误:
虽然是良性的,但它们很碍眼,可能指向我对 http 和异步代码缺乏了解,所以我宁愿尝试修复它。 然而,我不知道是什么原因造成的,尤其是我喜欢我说的代码运行得很好,无论如何。
如果您想更实际地尝试重新创建它,完整的代码在我的 github 存储库上的 dev 分支上: https://github.com/ohitstom/spicetify-easyinstall/tree/dev
如果您正在对此进行测试,大多数程序都可以忽略,只需按下安装按钮,有问题的代码就会在最后显示出来。 请记住,这是一个 spotify 主题,因此如果您安装了 spotify/spicetify,您将需要使用 vm。
Asyncio 和 aiohttp 在 Windows 上同时运行大量任务时会出现一些问题,我最近遇到了很多问题。
有一些解决方法可用,我最常用的是:
# set this before your event loop initialization or main function
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
或者:
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
loop.run_until_complete(your_main())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.