1

I have the following code which is running fine:

import concurrent.futures

NUMBERS = [1, 2, 3, 4, 5, 6, 7, 8]

def is_even(n):
    if n % 2 == 0:
        print(n, " is even number")
    else:
        print(n, " is not even number")

with concurrent.futures.ProcessPoolExecutor() as executor:
    result = executor.map(is_even, NUMBERS)

Output

$ python test_multiprocessing.py
1  is not even number
2  is even number
4  is even number
3  is not even number
5  is not even number
6  is even number
7  is not even number
8  is even number

But if I change the code to the following, which I think it should be the same:

import concurrent.futures

NUMBERS = [1, 2, 3, 4, 5, 6, 7, 8]

def main():
    def is_even(n):
        if n % 2 == 0:
            print(n, " is even number")
        else:
            print(n, " is not even number")

    with concurrent.futures.ProcessPoolExecutor() as executor:
        result = executor.map(is_even, NUMBERS)

if __name__ == '__main__':
    main()

Then run the code:

$ python test_multiprocessing.py
$

It does nothing without any output.

I expected the second piece of code should also work like the first piece, since I do not think there is any difference between the 2 pieces of code, but the second piece does not work. For sure I am still a Python beginner, and I would really appreciate it if someone can give me any hints. Thank you!

Mark
  • 11
  • 1

0 Answers0