Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 非阻塞控制台输入?_Python_Windows_Input - Fatal编程技术网

Python 非阻塞控制台输入?

Python 非阻塞控制台输入?,python,windows,input,Python,Windows,Input,我正在尝试用Python制作一个简单的IRC客户端(在我学习该语言的过程中作为一个项目) 我有一个循环,用来接收和解析IRC服务器发送给我的内容,但是如果我使用raw_input来输入内容,它会停止循环,直到我输入一些东西(显然) 如何在不停止循环的情况下输入内容 (我不认为我需要发布代码,我只想在1:循环停止时输入一些没有的内容。) 编辑:我在Windows上。对于Windows,仅控制台,请使用模块: 对于Linux,这描述了以下解决方案,它需要termios模块: import sys i

我正在尝试用Python制作一个简单的IRC客户端(在我学习该语言的过程中作为一个项目)

我有一个循环,用来接收和解析IRC服务器发送给我的内容,但是如果我使用
raw_input
来输入内容,它会停止循环,直到我输入一些东西(显然)

如何在不停止循环的情况下输入内容

(我不认为我需要发布代码,我只想在1:循环停止时输入一些没有
的内容。)


编辑:我在Windows上。

对于Windows,仅控制台,请使用模块:

对于Linux,这描述了以下解决方案,它需要
termios
模块:

import sys
import select
import tty
import termios

def isData():
    return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])

old_settings = termios.tcgetattr(sys.stdin)
try:
    tty.setcbreak(sys.stdin.fileno())

    i = 0
    while 1:
        print(i)
        i += 1

        if isData():
            c = sys.stdin.read(1)
            if c == '\x1b':         # x1b is ESC
                break

finally:
    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
对于跨平台,或者如果您也需要GUI,您可以使用Pygame:

import pygame
from pygame.locals import *

def display(str):
    text = font.render(str, True, (255, 255, 255), (159, 182, 205))
    textRect = text.get_rect()
    textRect.centerx = screen.get_rect().centerx
    textRect.centery = screen.get_rect().centery

    screen.blit(text, textRect)
    pygame.display.update()

pygame.init()
screen = pygame.display.set_mode( (640,480) )
pygame.display.set_caption('Python numbers')
screen.fill((159, 182, 205))

font = pygame.font.Font(None, 17)

num = 0
done = False
while not done:
    display( str(num) )
    num += 1

    pygame.event.pump()
    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        done = True

在Linux上,这里对mizipzor的代码进行了重构,使之变得更容易,以防您必须在多个地方使用此代码

import sys
import select
import tty
import termios

class NonBlockingConsole(object):

    def __enter__(self):
        self.old_settings = termios.tcgetattr(sys.stdin)
        tty.setcbreak(sys.stdin.fileno())
        return self

    def __exit__(self, type, value, traceback):
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, self.old_settings)


    def get_data(self):
        if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
            return sys.stdin.read(1)
        return False
下面是使用方法:此代码将打印一个不断增长的计数器,直到您按ESC键

with NonBlockingConsole() as nbc:
    i = 0
    while 1:
        print i
        i += 1
        if nbc.get_data() == '\x1b':  # x1b is ESC
            break

下面是一个在linux和windows下使用单独线程运行的解决方案:

import sys
import threading
import time
import Queue

def add_input(input_queue):
    while True:
        input_queue.put(sys.stdin.read(1))

def foobar():
    input_queue = Queue.Queue()

    input_thread = threading.Thread(target=add_input, args=(input_queue,))
    input_thread.daemon = True
    input_thread.start()

    last_update = time.time()
    while True:

        if time.time()-last_update>0.5:
            sys.stdout.write(".")
            last_update = time.time()

        if not input_queue.empty():
            print "\ninput:", input_queue.get()

foobar()
这是我见过的最棒的1。在此处粘贴,以防链接关闭:

#!/usr/bin/env python
'''
A Python class implementing KBHIT, the standard keyboard-interrupt poller.
Works transparently on Windows and Posix (Linux, Mac OS X).  Doesn't work
with IDLE.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as 
published by the Free Software Foundation, either version 3 of the 
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

'''

import os

# Windows
if os.name == 'nt':
    import msvcrt

# Posix (Linux, OS X)
else:
    import sys
    import termios
    import atexit
    from select import select


class KBHit:

    def __init__(self):
        '''Creates a KBHit object that you can call to do various keyboard things.
        '''

        if os.name == 'nt':
            pass

        else:

            # Save the terminal settings
            self.fd = sys.stdin.fileno()
            self.new_term = termios.tcgetattr(self.fd)
            self.old_term = termios.tcgetattr(self.fd)

            # New terminal setting unbuffered
            self.new_term[3] = (self.new_term[3] & ~termios.ICANON & ~termios.ECHO)
            termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.new_term)

            # Support normal-terminal reset at exit
            atexit.register(self.set_normal_term)


    def set_normal_term(self):
        ''' Resets to normal terminal.  On Windows this is a no-op.
        '''

        if os.name == 'nt':
            pass

        else:
            termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old_term)


    def getch(self):
        ''' Returns a keyboard character after kbhit() has been called.
            Should not be called in the same program as getarrow().
        '''

        s = ''

        if os.name == 'nt':
            return msvcrt.getch().decode('utf-8')

        else:
            return sys.stdin.read(1)


    def getarrow(self):
        ''' Returns an arrow-key code after kbhit() has been called. Codes are
        0 : up
        1 : right
        2 : down
        3 : left
        Should not be called in the same program as getch().
        '''

        if os.name == 'nt':
            msvcrt.getch() # skip 0xE0
            c = msvcrt.getch()
            vals = [72, 77, 80, 75]

        else:
            c = sys.stdin.read(3)[2]
            vals = [65, 67, 66, 68]

        return vals.index(ord(c.decode('utf-8')))


    def kbhit(self):
        ''' Returns True if keyboard character was hit, False otherwise.
        '''
        if os.name == 'nt':
            return msvcrt.kbhit()

        else:
            dr,dw,de = select([sys.stdin], [], [], 0)
            return dr != []


# Test    
if __name__ == "__main__":

    kb = KBHit()

    print('Hit any key, or ESC to exit')

    while True:

        if kb.kbhit():
            c = kb.getch()
            if ord(c) == 27: # ESC
                break
            print(c)

    kb.set_normal_term()
一,
制作人,他在.python3.3及更高版本下编写并发布的一部分。您可以使用本答案中提到的
asyncio
模块。 但要使用
asyncio
,您必须重新考虑代码。

我认为诅咒图书馆能帮上忙

import curses
import datetime

stdscr = curses.initscr()
curses.noecho()
stdscr.nodelay(1) # set getch() non-blocking

stdscr.addstr(0,0,"Press \"p\" to show count, \"q\" to exit...")
line = 1
try:
    while 1:
        c = stdscr.getch()
        if c == ord('p'):
            stdscr.addstr(line,0,"Some text here")
            line += 1
        elif c == ord('q'): break

        """
        Do more things
        """

finally:
    curses.endwin()

我会照米奇·陈说的做,但我会用
unicurses
而不是普通的诅咒。
Unicurses
是通用的(适用于所有或至少几乎所有的操作系统)

我最喜欢的非阻塞输入是在线程中使用python input()

import threading

class KeyboardThread(threading.Thread):

    def __init__(self, input_cbk = None, name='keyboard-input-thread'):
        self.input_cbk = input_cbk
        super(KeyboardThread, self).__init__(name=name)
        self.start()

    def run(self):
        while True:
            self.input_cbk(input()) #waits to get input + Return

showcounter = 0 #something to demonstrate the change

def my_callback(inp):
    #evaluate the keyboard input
    print('You Entered:', inp, ' Counter is at:', showcounter)

#start the Keyboard thread
kthread = KeyboardThread(my_callback)

while True:
    #the normal program executes without blocking. here just counting up
    showcounter += 1

与操作系统无关,仅内部库,支持多字符输入

以下是围绕上述解决方案之一的类包装器:

#/usr/bin/env蟒蛇3
导入线程
导入队列
类非阻塞输入:
定义初始(自我,退出条件):
self.exit\u条件=exit\u条件
self.input_queue=queue.queue()
self.input_thread=threading.thread(target=self.read_kbd_input,args=(),daemon=True)
self.input_thread.start()
def read_kbd_输入(自):
已完成\u排队\u输入=错误
未完成时\u排队\u输入:
控制台输入=输入()
self.input\u queue.put(控制台输入)
如果console_input.strip()=self.exit_条件:
已完成\u排队\u输入=真
def输入_排队(自):
返回值=False
如果self.input_queue.qsize()大于0:
返回值=真
返回值
def输入_获取(自身):
return_value=“”
如果self.input_queue.qsize()大于0:
return\u value=self.input\u queue.get()
返回值
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
非阻塞输入=非阻塞输入(退出条件='quit')
已完成\u处理=错误
输入_STR=“”
在未完成处理时:
如果非块输入。输入排队():
INPUT\u STR=非块输入。INPUT\u get()
如果INPUT_STR.strip()=“退出”:
已完成\u处理=真
其他:
打印(“{}”。格式(输入)
由于我发现其中一种方法很有用,下面是一个类似方法的示例。此代码在输入时创建节拍器效果

不同之处在于,这段代码使用闭包而不是类,这让我感觉有点直截了当。此示例还包含一个标志,用于通过
my_thread.stop=True
终止线程,但不使用全局变量。我通过(ab)使用python函数是对象这一事实来实现这一点,因此可以对其进行猴子修补,甚至可以从内部修补

注意:应小心停止螺纹。如果您的线程有需要某种清理进程的数据,或者如果该线程生成了自己的线程,那么这种方法将不客气地杀死这些进程

# Begin metronome sound while accepting input.
# After pressing enter, turn off the metronome sound.
# Press enter again to restart the process.

import threading
import time
import winsound  # Only on Windows

beat_length = 1  # Metronome speed


def beat_thread():
    beat_thread.stop = False  # Monkey-patched flag
    frequency, duration = 2500, 10
    def run():  # Closure
        while not beat_thread.stop:  # Run until flag is True
            winsound.Beep(frequency, duration)
            time.sleep(beat_length - duration/1000)
    threading.Thread(target=run).start()


while True:
    beat_thread()
    input("Input with metronome. Enter to finish.\n")
    beat_thread.stop = True  # Flip monkey-patched flag
    input("Metronome paused. Enter to continue.\n\n")


我已经有了pygame,所以我会试试这个。谢谢不过,还有其他人有更好的解决方案吗?我想让它成为一个控制台。当输入通过其他进程进行管道传输时,有没有办法让它工作?注意:windows解决方案实际上并不检查stdin中是否有任何内容。它检查键盘上的某个键是否被按下,因此管道输入将不会得到处理。我一直在寻找一种适当的方法来中断阻止input()的线程,但使用msvcrt和getch,我没有从input()获得正确的箭头键历史行为、退格等。有没有一种方法可以轻松地保持控制台行为,但仍然能够中断input()上的线程?linux解决方案似乎对我不起作用:
termios.error:(25,“设备的ioctl不合适”)
联机
old\u settings=termios.tcgetattr(sys.stdin)
。有什么想法吗?你用的是什么网络模块?Twisted、sockets、asyncore?做到这一点:非阻塞、多线程示例:似乎是在windows cmd控制台和eclipse中都能工作的唯一解决方案!在
sys.stdout.write(“.”
)之后需要一个
sys.stdout.flush()
。mac呢?@ColorCodin也应该可以工作,因为它基于unixUsing GNU/Linux:输入字符后仍然需要按enter键,但它可以工作。至少它是非阻塞的,并且大部分返回正常字符(没有键码,当然除了特殊的键,如转义键或退格键)。谢谢诅咒是不可移植的。诅咒是完全可移植的。它实际上比Python本身更具可移植性。我认为这是最好的答案,但这取决于您的用例。例如,诅咒会在控制台可用之前清除它。这种方法完全符合我的需要。请看下面我的答案,这是一个使用闭包的更简洁的方法。是的,我觉得这应该是正确的答案,它是最不混乱和轻量级的。我不
# Begin metronome sound while accepting input.
# After pressing enter, turn off the metronome sound.
# Press enter again to restart the process.

import threading
import time
import winsound  # Only on Windows

beat_length = 1  # Metronome speed


def beat_thread():
    beat_thread.stop = False  # Monkey-patched flag
    frequency, duration = 2500, 10
    def run():  # Closure
        while not beat_thread.stop:  # Run until flag is True
            winsound.Beep(frequency, duration)
            time.sleep(beat_length - duration/1000)
    threading.Thread(target=run).start()


while True:
    beat_thread()
    input("Input with metronome. Enter to finish.\n")
    beat_thread.stop = True  # Flip monkey-patched flag
    input("Metronome paused. Enter to continue.\n\n")