6

I would like to write a simple program that both (1) produces lines of output simultaneously, and (2) accepts input from the user via a command line (via readline). (Think of a text-mode chat client, for example. I want to be able to compose my chat messages while still seeing incoming chat messages as they are received.) To accomplish this, I would like to be able to call readline asynchronously.

The Readline library explicitly supports this, via its callback 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.

  • Is this functionality available via Python?
  • Is it possible to use the Cmd class for such a purpose?
nibot
  • 14,428
  • 8
  • 54
  • 58

2 Answers2

0

Some installations of python support the readline interface, some do not. The only way to find out is via testing. Does import readline work or raise and ImportError?

The python cmd module is intended for a totally diferent purpose involvivng making your own shells and is not helpful in this context.

Even if the readline module is not available, a similar task could be accomplished with threads.

pppery
  • 3,731
  • 22
  • 33
  • 46
  • 1
    I don't think "cmd" is intended for a "totally diferent purpose". I want cmd, but with non-blocking input. – nibot Oct 22 '15 at 23:03
  • I do have readline. My question is how to use it to accomplish what I want. – nibot Oct 22 '15 at 23:04
0

No, cmd's use of readline does not include using the callback interface. You cannot accomplish what want using that.

However, you can accomplish what you want by using threading. Do your blocking network reads and printing of chat messages in one thread; do your blocking calls to cmd, the execution of cmds, and the network writes in another thread.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308