Seome kind of unblocking input

J

janis.judvaitis

Hello!

I'm building small console like program for embedded system control over serial port. Naturally I need to be able to recieve commands from user and print reply's from embedded device.

Since I'm using threads and pipes everything works ok, except that when i call input() there is no way that I could print something, is there any workaround for this??

Note: I don't need to catch any key's before enter or smtng, just be able to print while input() is waiting. I'm thinking that maybe there is a way for two threads to share one stdout, which should resolve this, but I can't make it work, since U can't pickle file like object(stdout) to pass it to other thread.

Note 2: I've readed about ways to make nonblocking input by reading single char etc. but that's is all messy and very platform dependent, I would loveto have platform independent solution.

Thanks in advance!

--JJ
 
D

Dennis Lee Bieber

Since I'm using threads and pipes everything works ok, except that when i call input() there is no way that I could print something, is there any workaround for this??

Note: I don't need to catch any key's before enter or smtng, just be able to print while input() is waiting. I'm thinking that maybe there is a way for two threads to share one stdout, which should resolve this, but I can't make it work, since U can't pickle file like object(stdout) to pass it to other thread.

Confusion abounds...

You don't have to "pickle file like object..." for it to be used by
a Python THREAD... But your mention of pipes makes me think you are
using subprocesses and/or multiprocessing modules. Threads run in a
shared environment (you may need to put a lock around the file object so
that only one thread at a time does the I/O on that object), but
processes are independent memory spaces.

However, you may also encounter OS specific behavior WRT
stdout/stderr when they are connected to a console. The OS itself may
block/buffer output when there is a pending input on the same console.
 
R

Ramchandra Apte

Confusion abounds...



You don't have to "pickle file like object..." for it to be used by

a Python THREAD... But your mention of pipes makes me think you are

using subprocesses and/or multiprocessing modules. Threads run in a

shared environment (you may need to put a lock around the file object so

that only one thread at a time does the I/O on that object), but

processes are independent memory spaces.



However, you may also encounter OS specific behavior WRT

stdout/stderr when they are connected to a console. The OS itself may

block/buffer output when there is a pending input on the same console.

--

Wulfraed Dennis Lee Bieber AF6VN

(e-mail address removed) HTTP://wlfraed.home.netcom.com/

You can clear the buffer by calling file.flush()
 
R

Ramchandra Apte

Confusion abounds...



You don't have to "pickle file like object..." for it to be used by

a Python THREAD... But your mention of pipes makes me think you are

using subprocesses and/or multiprocessing modules. Threads run in a

shared environment (you may need to put a lock around the file object so

that only one thread at a time does the I/O on that object), but

processes are independent memory spaces.



However, you may also encounter OS specific behavior WRT

stdout/stderr when they are connected to a console. The OS itself may

block/buffer output when there is a pending input on the same console.

--

Wulfraed Dennis Lee Bieber AF6VN

(e-mail address removed) HTTP://wlfraed.home.netcom.com/

You can clear the buffer by calling file.flush()
 
M

Mark Lawrence

You can clear the buffer by calling file.flush()

Shock, horror, probe, well I never did. I'm sure that everyone is
updating their Xmas and birthday card lists to ensure that you're not
missed out having furnished a piece of information that doubtless not
one other person on this group knew.
 
C

Chris Angelico

It probably wouldn't have helped either... The OP was looking for,
as I recall, some means by which a pending input would not block other
output on a console... So what use is flushing a buffer?

It all depends on *why* pending input appears to be blocking other
output. I say "appears to be" because buffered output can indeed
appear to be blocked, but so can other things.

Flushing output is an easy thing to try. If it fails, back to square
one, but with more knowledge.

ChrisA
 
R

Ramchandra Apte

On Sun, 23 Sep 2012 18:13:57 +0100, Mark Lawrence

<[email protected]> declaimed the following in

gmane.comp.python.general:









It probably wouldn't have helped either... The OP was looking for,

as I recall, some means by which a pending input would not block other

output on a console... So what use is flushing a buffer?

--

Wulfraed Dennis Lee Bieber AF6VN

(e-mail address removed) HTTP://wlfraed.home.netcom.com/


Okay okay! My message was out-of-context.
Anyways, nobody in my age group knows that you can clear the buffer of a file in Python by calling file.flush().
 
J

janis.judvaitis

Thanks for reply's. I'll be looking into threading, it seems like right way to go.

btw. Why Python developers don't make a wrapper for input() with callback function using threads, so people can easily use nonblocking input?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top