'how to force input() to stop and continue the code flow

I need to exit a loop that contains an input statement even if the user hasn't written anything. It receives parameters from a process too, and it must evaluate the content immediately. Something like this:

import multiprocessing

def my_function (my_queue):
    var = ""
    #### some code which finally puts something in the queue ###
    my_queue.put(var)


def main():
    my_queue = multiprocessing.Queue()

    p1 = multiprocessing.Process (target=my_function, args =(my_queue,))
    p1.daemon = True
    p1.start()

    my_var = ""
    
    while (my_queue.empty() is True and my_var == ""):
        my_var = input ("enter a parameter for my_var: ")

    #### code that evaluates the queue and the input as appropiate

## I want to exit the loop if there's something in the queue even if the user hasn't written anything

This doesn't work of course. The main loop is stacked in the input part. Any ideas? I'm using Windows. Thank you all in advance!



Solution 1:[1]

Try this: we send a signal from the child to the parent process using os.kill, which raises an exception that we catch in order to escape the input function.

import multiprocessing
import signal
import time
import os


def my_function (my_queue, pid):
    var = ""
    #### some code which finally puts something in the queue ###
    time.sleep(3)
    my_queue.put(var)
    os.kill(pid, signal.SIGUSR1)


def interrupted(*args):
    print('Item added to queue before user input completed.')
    raise InterruptedError


def main():
    my_queue = multiprocessing.Queue()

    p1 = multiprocessing.Process (target=my_function, args =(my_queue,
                                                             os.getpid()))
#     p1.daemon = True
    p1.start()

    my_var = ""
    try:
        signal.signal(signal.SIGUSR1, interrupted)
        while (my_queue.empty() is True and my_var == ""):
            my_var = input ("enter a parameter for my_var: ")
    except InterruptedError:
        pass
    print('Processing results...')
    #### code that evaluates the queue and the input as appropiate

## I want to exit the loop if there's something in the queue even if the user hasn't written anything
if __name__ == '__main__':
    main()

The code above only works on Unix. I don't know whether the following code will work cross-platform, but it might:

import multiprocessing
import signal
import time
import os


def my_function (my_queue, pid):
    var = ""
    #### some code which finally puts something in the queue ###
    time.sleep(3)
    my_queue.put(var)
    os.kill(pid, signal.SIGINT)

def interrupted(*args):
    raise InterruptedError


def main():
    print('I am %s!' % os.getpid())
    my_queue = multiprocessing.Queue()

    p1 = multiprocessing.Process (target=my_function, args =(my_queue,
                                                             os.getpid()))
    p1.daemon = True
    p1.start()

    my_var = ""
    try:
        signal.signal(signal.SIGINT, interrupted)
        while (my_queue.empty() is True and my_var == ""):
            my_var = input ("enter a parameter for my_var: ")
    except InterruptedError:
        time.sleep(0.1)
        if my_queue.empty():
            print('Exiting gracefully...')
            return
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    print('Processing results...')
    time.sleep(10)
    #### code that evaluates the queue and the input as appropiate

## I want to exit the loop if there's something in the queue even if the user hasn't written anything
if __name__ == '__main__':
    main()

Solution 2:[2]

Yes, You can do it by adding styles for navbar and jumbotron.

.color,.navbar .navbar-default{
  background-image: url("flower.jpg");
}

Add color as a class in Jumbotron.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 lakshna S