0

I'm writting a python script which receive several parameters on his standard input. I use the raw_input() function which works for the firsts parameters but totally freeze when I call it inside a while loop.

Here is my code:

def launch_trade(logger):
    Kerviel = Trader()
    param = raw_input() #Works fine
    Kerviel.Capital = float(param)
    param = raw_input() #Works fine
    Kerviel.NbDays = int(param)
    param = raw_input() #Works Fine
    while (param != '--end--'):
        Kerviel.action(float(param), logger)
        Kerviel.Cours.append(param)
        param = raw_input() #Here it infinite wait

Actually this program works when I send all parameters myself in my console. But it is supposed to be called by a php script which sends it parameters on his stdin.

Why does this last raw_input() doesn't work when parameters are sent by a php script ?

Thanks for your answers and sorry for bad english.

1 Answer 1

1

Each input or raw_input call waits for a line ending with '\n'. A live user hits keys ending with the Enter key. A program has to send strings to stdout, connected to stdin, ending with '\n'. So the problem must be that the php program is not sending enough, or that not eveything it sends gets to your python program. If the latter, it could be that the php program needs to 'flush' stdout to push the last string to python. When a file is closed, flush is automatic, but stdout would not ordinarily be closed.

Sign up to request clarification or add additional context in comments.

3 Comments

The problem is I can't modify a single line in the PHP part... We are many to work with the same PHP script and I'm the only one who have problems to get the input... So I can be sure the problem comes from me.
Are others getting the input through a pipe? Or only from a file written by php?
They are all using functions like getline() (C++) or equivalents in many different languages

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.