我目前正在尝试制造一个启动selenium线程的不和谐的机器人。它可以工作,但唯一的问题是,如果selenium花费的时间太长,我就不能使用其他命令。它最终会响应下一个命令,但只有在selenium完成之后才会进行响应。
这就是我所拥有的:
import threading
import discord
import time
from selenium import webdriver
from discord.ext import tasks, commands
client = commands.Bot(command_prefix='!')
def start(url):
driver = webdriver.Firefox()
driver.get(url)
time.sleep(10)
driver.close()
@client.command()
async def rq(ctx):
#if link == None:
#await ctx.send("Please send a link!")
await ctx.send("Started!")
threading(target=start("https://google.com/")).start()
@client.command()
async def sc(ctx):
await ctx.send("some command")
if __name__ == "__main__":
client.run(token)任何解决方案都是有帮助的!
发布于 2021-09-11 14:23:29
调用线程的方式是不正确的:
threading(target=start("https://google.com/")).start()这样做的目的是:
start函数,传递给它URL。None)作为target函数传递给线程构造函数(顺便说一下,您的意思是d10那里)。H 211G 212因此,当线程开始时,主线程上的实际工作已经完成,而线程本身却什么也不做。
启动线程并传递一些参数的正确方法是:
threading.Thread(target=start, args=("https://google.com/",)).start()注意start后面没有跟着(),所以我们不是直接调用函数,而是将函数本身传递给Thread构造函数。参数作为元组给出args参数(因此后面的逗号)。
https://stackoverflow.com/questions/69143789
复制相似问题