如何在不阻塞的情况下从控制台获取输入?

时间:2019-08-28 08:44:54

标签: python python-3.x blocking discord.py-rewrite console-input

我想发出命令将消息发送到文本通道或用户,并且应该在控制台上输入消息,但是input()似乎在程序中“ 阻止”。尽管我曾尝试通过线程找出问题的根源,但问题仍未解决。如何解决该问题?

像这样的简单代码:

import discord
import typing
from discord.ext import commands

class BasicCmd(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command()
    async def test(self, ctx, *, receiver: typing.Union[discord.User, discord.TextChannel]):
        '''
        I want to get input from console
        but input() block the program.
        '''
        content = input('Enter something: ')
        await receiver.send(content)

def setup(client):
    client.add_cog(BasicCmd(client))

如果我使用线程来做:

import discord
import typing
import threading
from discord.ext import commands

class BasicCmd(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command()
    async def test(self, ctx, *, receiver: typing.Union[discord.User, discord.TextChannel]):
        '''
        I want to do this command with threading to figure out  
        this problem,but the problem still remain unsolve.
        '''
        content = threading.Thread(target=input,args=('Enter something: ',)).start()
        await receiver.send(content)
        '''It will  raise an HTTPException(Cannot send an enpty message,and the bot also "block" by "input")'''

def setup(client):
    client.add_cog(BasicCmd(client))

我已经修改了程序,但是输入仍然阻止了我的程序。 代码(只需测试):

import discord
import typing
import queue
import threading
from discord.ext import commands

class BasicCmd(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command()
    async def test(self, ctx):
        inputQueue = queue.Queue()

        inputThread = threading.Thread(target=_read_kbd_input, args=(inputQueue,), daemon=True)
        # It still remain "blocking"
        inputThread.start()

        while (True):
            if (inputQueue.qsize() > 0):
                input_str = inputQueue.get()
                print("input_str = {}".format(input_str))
                return

def _read_kbd_input(inputQueue):
    print('Ready for keyboard input:')
    a=True
    while a:
        input_str = input()
        inputQueue.put(input_str)
        a=False
def setup(client):
    client.add_cog(BasicCmd(client))

1 个答案:

答案 0 :(得分:0)

为输入创建一个新的异步定义

async def noinput():
  global inputoutput, askinput
  while True:
    if askinput==True
       inputoutput=input('input: ')
       askinput==False

那么,你必须先运行它,然后才能在底部运行你的机器人

client.loop.create_task(noinput())
client.run(token)

你的输入询问者应该是这样的

@client.event
async def on_message(message: discord.Message):
  global askinput, inputoutput
  if message.content.startswith("input"):
    askinput==True
    while askinput==True:
      await asyncio.sleep(1)
    output=inputoutput
    #do something with output

示例代码

async def noinput():
  global inputoutput, askinput
  while True:
    if askinput==True
       inputoutput=input('input: ')
       askinput==False

client=discord.Client()
async def on_message(message: discord.Message):
  global askinput, inputoutput
  if message.content.lower() == "input":
    askinput==True
    while askinput==True:
      await asyncio.sleep(1)
    message.channel.send(inputoutput)
client.loop.create_task(noinput())
client.run(token)