Maybe you should explain this:
real time input works
Batch programs do not have real time input.
They are batch programs.
By definition they are background tasks.
Explain what you think the program does. Then we can focus on just that little bit of code that does what you describe. That code is much to large to post here. Tell us what you think it does and somebody here with exact the few lines of code that do what you think it does.