以编程方式中断raw_input [英] Programmatically interrupting raw_input

查看:61
本文介绍了以编程方式中断raw_input的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法以编程方式中断Python的raw_input?具体来说,我想向用户显示一个提示,但是还要监听套接字描述符(例如,使用select)并中断该提示,输出一些内容,如果套接字上有数据,则重新显示该提示.

Is there a way to programmatically interrupt Python's raw_input? Specifically, I would like to present a prompt to the user, but also listen on a socket descriptor (using select, for instance) and interrupt the prompt, output something, and redisplay the prompt if data comes in on the socket.

使用raw_input而不是简单地在sys.stdin上进行选择的原因是,我想使用readline模块为提示提供行编辑功能.

The reason for using raw_input rather than simply doing select on sys.stdin is that I would like to use the readline module to provide line editing functionality for the prompt.

推荐答案

据我所知...排序".

As far as I know... "Sort of".

raw_input正在阻塞,所以我唯一想到的方法是产生一个子进程/线程以检索输入,然后简单地与该线程/子进程进行通信.这是一个相当肮脏的技巧(至少在我看来是如此),但是它应该可以跨平台工作.当然,另一种选择是使用linux上的curses模块或获取此版本对于Windows.

raw_input is blocking so the only way I can think of is spawning a subprocess/thread to retrieve the input, and then simply communicate with the thread/subprocess. It's a pretty dirty hack (at least it seems that way to me), but it should work cross platform. The other alternative, of course, is to use either the curses module on linux or get this one for windows.

这篇关于以编程方式中断raw_input的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆