如何使readLine()超时 [英] How to make readLine() timeout

查看:69
本文介绍了如何使readLine()超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序基本上是具有所有预期功能(如提示符,历史记录等)的CLI,它需要等待STDIN进行用户输入.为此,我正在使用readLine系统调用.我创建了一个网络套接字,该网络套接字用于将用户读取的消息发送到服务器.我的应用程序是单线程的.因为我的应用程序被用户输入阻止了,所以创建的套接字没有响应来自服务器的保持活动消息.我想知道一段时间后是否有办法使readLine超时,以便我只对网络套接字进行轮询,然后返回以等待readLine?.

My application basically is a CLI with all the expected features like prompt, history etc., it needs to wait on STDIN for user input. For this I am using readLine system call. I have a created a network socket which is used to send the message read from user to server. My application is single threaded one. Because my application is blocked for user input, the socket created is not responding to keep-alive messages from server. I want to know if there is a way make readLine timeout after some time, so that I will just poll on my network socket and come back to wait on readLine?.

我知道有一个解决方案,我可以生成一个线程来处理网络操作.但是我不想让我的应用程序成为多线程.

I know there is one solution where I can spawn a thread to take care of network operations. But I dont want to make my app multithreaded.

推荐答案

我正在使用readLine系统调用.

I am using readLine system call.

您提供的信息不一致.系统调用 readLine 是什么系统?
如果您使用的问题,则是在调用库函数.但是,根据GNU Readline库,函数和变量索引,没有功能 readLine ,只有

You provide inconsistent information. What system would that be that has a system call readLine?
If you use libreadline, you are rather calling library functions. But then, according to the GNU Readline Library Function and Variable Index, there is no function readLine, there's only readline. With it you could use either the

变量: rl_hook_func_t * rl_event_hook
如果非零,则为Readline等待时定期调用的函数的地址用于终端输入.默认情况下,最多调用十次如果没有键盘输入,请等待一秒钟.

Variable: rl_hook_func_t * rl_event_hook
If non-zero, this is the address of a function to call periodically when Readline is waiting for terminal input. By default, this will be called at most ten times a second if there is no keyboard input.

(您将其设置为轮询网络套接字并响应消息的功能)

(you'd set it to a function where you poll your network socket and respond to messages)

备用界面
普通 readline()可以使用备用接口.某些应用程序通常需要使用主循环在各种文件描述符上对 select()进行编码,从而使键盘I/O与文件,设备或窗口系统I/O交错.为了满足这种需求,还可以从事件循环中将readline作为回调"函数来调用.有一些功能可以简化此操作.

Alternate Interface
An alternate interface is available to plain readline(). Some applications need to interleave keyboard I/O with file, device, or window system I/O, typically by using a main loop to select() on various file descriptors. To accommodate this need, readline can also be invoked as a `callback' function from an event loop. There are functions available to make this easy.

有一个使用备用界面的示例程序:备用界面示例.

There's an example program using the alternate interface: Alternate Interface Example.

这篇关于如何使readLine()超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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