【问题标题】:RuntimeError: Event loop stopped before Future completed. PythonRuntimeError:事件循环在 Future 完成之前停止。 Python
【发布时间】:2021-03-23 06:06:28
【问题描述】:

所以我正在尝试制作一个我的世界名称狙击手,但它抛出了这个错误:

RuntimeError: Event loop stopped before Future completed.

我的代码:

import asyncio
import aiohttp
tokenlist = ['token1','token2','token3','token4','token5']
name = 'bruh'

async def send_requests():
    coros = [
            snipe_req(token) for token in tokenlist for x in range(3)

            ]
    await asyncio.wait(coros)

async def snipe_req(token):
    await asyncio.sleep(0)
    async with aiohttp.ClientSession().put(f"https://api.minecraftservices.com/minecraft/profile/name/{name}",headers={"Authorization": "Bearer " + token,"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0","Content-Type": "application/json"}) as response:
        await response.read()
        print(response)
        asyncio.get_event_loop().stop()

def run():
    loop = asyncio.get_event_loop()

    loop.run_until_complete(send_requests())

run()

有人知道问题出在哪里吗?

【问题讨论】:

  • 为什么是asyncio.get_event_loop().stop()?你正在停止事件循环

标签: python python-asyncio


【解决方案1】:

你必须先打开会话,然后使用 put 方法。

import asyncio
import aiohttp

tokenlist = ['token1','token2','token3','token4','token5']
name = 'bruh'

async def send_requests():
    coros = [
            snipe_req(token) for token in tokenlist for x in range(3)

            ]
    await asyncio.wait(coros)

async def snipe_req(token):
    async with aiohttp.ClientSession() as client:
        session = await client.put(
        f"https://api.minecraftservices.com/minecraft/profile/name/{name}",
        headers={
            "Authorization": "Bearer " + token,
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0",
            "Content-Type": "application/json"
            })
        await session.read()
        print(session)

def run():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(send_requests())

run()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 2014-12-31
    相关资源
    最近更新 更多