A.L. Verminburger
A.L. Verminburger

Reputation: 106

Custom user input prompt (to object constructor) does not appear / appears sporadically when called from a separate thread

Have a function of the form:

def setup_my_object():
    my_object = My_Object()
    my_object_daemon = Pyro4.core.Daemon(port=55666)
    Pyro4.Daemon.serveSimple({my_object: "my.object"},ns = False,daemon = my_object_daemon)

Pyro4 library allows to access the object over the network. Because the main process creates several different objects, a separate thread is created using:

def main():
    threaded_object = threading.Thread(target = setup_my_object)
    threaded_object.start()

The object is of the form (in reality constructor is more complicated).

class My_Object(object):
    def __init__(self):
        name_option = input('\nDo you want to enter a name? [y/n]:\n')
        if (name_option == 'y')
            self.m_name = add_name()

    def add_name(self):
        name = input('\nPlease enter the name: \n')
        return(name)

The main() runs on a linux server, launched from a python console. The problem is when I launch main() the console never promts me "Do you want to enter a name?". I migh hit enter - wait for 30 seconds - nothing. Hit enter two times - wait for 30 seconds nothing. Only when I click enter like five times (and inadvertently the sixth) it will display "Do you want to enter a name?". What is going on and how do I avoid this, i.e. get an instant printout of "Do you want to enter a name?"?

Additional info: I am not seeing this problem when launching on a Windows machine; the problem is only on a Linux machine.

Upvotes: 0

Views: 53

Answers (1)

Irmen de Jong
Irmen de Jong

Reputation: 2847

The problem may be that you're doing input and output to stdin/stdout from different threads. Threads and stdin/stdout don't work nicely together. Imagine 4 threads that all sit waiting in their input... and you press enter... what thread will see your keystroke? That's basically random. Same with their output; multiple threads writing to stdout can produce very weird results.

It doesn't explain the large delay though. You haven't shown all your code. What does main() do more? If you replace the Pyro calls that start the daemon by some print statements, does the issue go away? Basically: figure out exactly where the cause of your issue is (what line of code) and continue from there

Upvotes: 0

Related Questions