在python[Discord.py]中同时运行多个协程

2024-09-30 16:20:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的朋友制作一个discord机器人,它会检查网站上是否有库存,如果发现库存,会向discord频道发送消息。我试图同时运行ne和bB方法,并且彼此独立。当“wait channel.send”行在各自的方法中运行时,我收到“Timeout context manager应在任务中使用”。此行应发送消息,以使discord bot向指定的通道发送消息。否则就可以了。我曾尝试按照discord文档将它们作为任务循环运行,但没有成功。我还广泛研究了各种替代方法,以实现相同的目标,但一直走到死胡同。在一个.py文件中是否有实现这一点的方法,或者我是否需要重新编写。诚然,我可能也不太理解线程或协同程序是如何工作的,但我自己也做了很多研究,现在正在寻求外界的帮助

这是我的密码。(我已审查了可能的私人信息,如discord频道ID)


import time
import requests
import re
import discord
import threading
import random
import asyncio
from discord.ext import tasks


bbUrlList = []
neUrlList = []
neGpuList = []
neSkipList = {}

client = discord.Client()

with open("DiscordToken.txt",'r') as tokenFile:
    token = str(tokenFile.readline()).replace("Token=","")

with open("bb.txt",'r') as file:
        for line in file:
            bbUrlList.append(line.split(" ")[1])

with open("ne.txt",'r') as file:
        for line in file:
            neUrlList.append(line.split(" ")[1])
            neGpuList.append(line.split(" ")[0])

async def bB(channel):
    while True:
        for x in range(0,len(bbUrlList)):
            url = bbUrlList[x]
            headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'}
            request = requests.get(url, headers=headers)
            request.encoding = "utf-8"
            text = request.text
            if re.search("add to cart", text, re.IGNORECASE) or re.search("see details", text, re.IGNORECASE):
                await channel.send("Bb appears to be dropping NOW!")
                print("[POSSIBLE DROP] Link:", bbUrlList[x])
                time.sleep(1800)
            else:
                print("[OUT OF STOCK] Link:", bbUrlList[x])
                time.sleep(random.randint(1,3))

async def ne(channel):
    while True:
        for x in range(0,len(neUrlList)):
            url = neUrlList[x]
            gpu = neGpuList[x]
            headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'}
            request = requests.get(url, headers=headers)
            request.encoding = "utf-8"
            text = request.text
            if re.search("add to cart", text, re.IGNORECASE):
                if url in neSkipList:
                    print("[REPEAT STOCK] Link:", url, neSkipList[url], "Rotations left.")
                    neSkipList[url] -= 1
                    if neSkipList[url] <= 0:
                        neSkipList.pop(url)
                    time.sleep(random.randint(1,3))
                    continue
                await channel.send("There might be a {} at {}".format(neGpuList[x], neUrlList[x]))
                print("[POSSIBLE STOCK] Link:", neUrlList[x])
                neSkipList[url] = 2
                time.sleep(random.randint(1,3))
            else:
                print("[OUT OF STOCK] Link:", neUrlList[x])
                time.sleep(random.randint(1,3))
                
@client.event            
async def on_ready():
    channel = client.get_channel(################)
    threading.Thread(target=asyncio.run, args = (bB(channel),)).start()
    threading.Thread(target=asyncio.run, args = (ne(channel),)).start()

##Also tried the following
##asyncio.run_coroutine_threadsafe(ne(channel), #Something Here#)
##asyncio.run_coroutine_threadsafe(bB(channel), #Something Here#)



client.run(token)


Tags: textimportreasynciourltimerequestchannel
1条回答
网友
1楼 · 发布于 2024-09-30 16:20:58

我认为这有两个主要原因,第一个原因是requests模块是为同步IO构建的,不能与异步一起正常工作。[See here for more]您可以尝试用类似^{}的东西替换requests模块

第二,time.sleep()是阻塞,asyncio.sleep()是非阻塞。在异步循环的上下文中,asyncio将只告诉特定线程等待,而您的整个代码将被允许继续

在您的代码中,这将被放在一起,如下所示:

import time
import re
import discord
import threading
import random
import asyncio
import aiohttp
from discord.ext import tasks


bbUrlList = []
neUrlList = []
neGpuList = []
neSkipList = {}

client = discord.Client()

with open("DiscordToken.txt",'r') as tokenFile:
    token = str(tokenFile.readline()).replace("Token=","")

with open("bb.txt",'r') as file:
        for line in file:
            bbUrlList.append(line.split(" ")[1])

with open("ne.txt",'r') as file:
        for line in file:
            neUrlList.append(line.split(" ")[1])
            neGpuList.append(line.split(" ")[0])

async def bB(channel):
    async with aiohttp.ClientSession() as session:
        while True:
            for x in range(0,len(bbUrlList)):
                url = bbUrlList[x]
                headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'}
                async with session.get(url, headers=headers) as request:
                    text = await request.text()
                    if re.search("add to cart", text, re.IGNORECASE) or re.search("see details", text, re.IGNORECASE):
                        await channel.send("Bb appears to be dropping NOW!")
                        print("[POSSIBLE DROP] Link:", bbUrlList[x])
                        await asyncio.sleep(1800)
                    else:
                        print("[OUT OF STOCK] Link:", bbUrlList[x])
                        await asyncio.sleep(random.randint(1,3))

async def ne(channel):
    async with aiohttp.ClientSession() as session:
        while True:
            for x in range(0,len(neUrlList)):
                url = neUrlList[x]
                gpu = neGpuList[x]
                headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'}
                async with session.get(url, headers=headers) as request:
                    text = await request.text()
                    if re.search("add to cart", text, re.IGNORECASE):
                        if url in neSkipList:
                            print("[REPEAT STOCK] Link:", url, neSkipList[url], "Rotations left.")
                            neSkipList[url] -= 1
                            if neSkipList[url] <= 0:
                                neSkipList.pop(url)
                            await asyncio.sleep(random.randint(1,3))
                            continue
                        await channel.send("There might be a {} at {}".format(neGpuList[x], neUrlList[x]))
                        print("[POSSIBLE STOCK] Link:", neUrlList[x])
                        neSkipList[url] = 2
                        await asyncio.sleep(random.randint(1,3))
                    else:
                        print("[OUT OF STOCK] Link:", neUrlList[x])
                        await asyncio.sleep(random.randint(1,3))
                
@client.event            
async def on_ready():
    channel = client.get_channel(################)
    threading.Thread(target=asyncio.run, args = (bB(channel),)).start()
    threading.Thread(target=asyncio.run, args = (ne(channel),)).start()


client.run(token)

我没有太多使用aiohttp的经验,所以一定要查看its documentation,以便根据您的具体需要对其进行定制

祝你的项目好运

相关问题 更多 >