Up

On Some Troubles with Input and MultiProcessing Class Pool

 

When using Pool in multiprocessing.pool module, location of function input is critical. Function input should be within the suite of if sentence if __name__ == e__mainf__. For example, the script in Listing 1, code input('If ready, press Enter-key') is within the suite.

Listing 1

from tkinter import *

from multiprocessing.pool import Pool

import time

 

def funcs( params ):

    root = Tk()

    canvas = Canvas(root, bg = '#ffffff', width = 400, height = 300)

    canvas.pack()

    canvas.create_text(200, 150, text = 'I am Proc-' + params, font = ('Gothic', 32), tags = 'mytext')

    canvas.update()

    time.sleep(3)

    root.destroy()

    return

 

if __name__ == '__main__':

    params = ['A', 'B']

    print('Are you ready ?')

    input('If ready, press Enter-key')

    pool = Pool()

    Results = pool.map(funcs, params)

 

Run the script in Listing 1, Pressing Enter-key is required (Figure 1).

Figure 1

 

Press Enter-key, then multiprocessing by Pool starts, and two forms are displayed (Figure 2).

Figure 2

 

After 3 seconds elapsed, the forms disappear and the program ends.

 

Now, move the input function to the outside of the suite, as shown in Listing 2.

Listing 2

from tkinter import *

from multiprocessing.pool import Pool

import time

 

def funcs( params ):

    root = Tk()

    canvas = Canvas(root, bg = '#ffffff', width = 400, height = 300)

    canvas.pack()

    canvas.create_text(200, 150, text = 'I am Proc-' + params, font = ('Gothic', 32), tags = 'mytext')

    canvas.update()

    time.sleep(3)

    root.destroy()

    return

 

print('Are you ready ?')

input('If ready, press Enter-key')

if __name__ == '__main__':

    params = ['A', 'B']

    pool = Pool()

    Results = pool.map(funcs, params)

 

When you run the script in Listing 2, forms like those in Figure 2 will not be presented after pressing Enter-key in the display in Figure 2. When the forms are not displayed after pressing Enter-key and nothing occurs, choose the menu item gShell|Restart Shellh as shown in Figure 3 to stop the execution.

Figure 3

 

What kind of trouble occurs depends on the environment in which the script is executed. When the script in Listing 2 is executed by a command in Windows PowerShell, several lines by input and print functions are displayed (Figure 4).

Figure 4

 

If you press Enter-key several times, execution of the script proceeds and the forms will be displayed.

 

Up