Notebook

これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。

HTML ファイル生成日時: 2026/03/09 21:11:11.534 (台灣標準時)

Python 3.14 の multiprocessing モジュールについて

以下の Python のコードを Python 3.14 で実行しようとしたら、うまくいか なかったでござる。


#!/usr/pkg/bin/python3.13

#
# Time-stamp: <2025/09/08 12:58:37 (UT+08:00) daisuke>
#

# importing multiprocessing module
import multiprocessing

# number of CPUs to be used
n_cpu = 4

# output file name
file_output = 'prime_numbers_4.data'

# start and end numbers
n_start = 2
n_end   = 10**5

# function to check whether or not the number is a prime number
def is_prime_number (x):
    # resetting parameter "pn"
    pn = 1
    # examining if the number is divisible by numbers between 2 and (x-1)
    for k in range (2, x):
        # if the number is divisible by k
        if (x % k == 0):
            # then subtract 1 from "pn"
            pn -= 1
            # leaving from the loop
            break
    # returning number and a flag "pn"
    return (x, pn)

# numbers to be checked
numbers = range (n_start, n_end + 1)

# making a pool of workers
pool = multiprocessing.Pool (n_cpu)

# executing function for different numbers in parallel
results = pool.map (is_prime_number, numbers)

# opening file for writing
with open (file_output, 'w') as fh_out:
    # for each number in the list "list_pn"
    for x, pn in results:
        # writing number to the file
        if (pn):
            fh_out.write (f'{x}\n')

以下のようなメッセージが表示されたでござる。


% python3.14 appy_s02_07_01.py 
Traceback (most recent call last):
  File "", line 1, in 
    from multiprocessing.forkserver import main; main(16, 17, ['__main__'], **{'sys_path': ['/tmp', '/usr/pkg/lib/python314.zip', '/usr/pkg/lib/python3.14', '/usr/pkg/lib/python3.14/lib-dynload', '/usr/pkg/lib/python3.14/site-packages'], 'main_path': '/tmp/appy_s02_07_01.py', 'authkey_r': 19})
                                                 ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/pkg/lib/python3.14/multiprocessing/forkserver.py", line 217, in main
    spawn.import_main_path(main_path)
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/usr/pkg/lib/python3.14/multiprocessing/spawn.py", line 307, in import_main_path
    _fixup_main_from_path(main_path)
    ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/usr/pkg/lib/python3.14/multiprocessing/spawn.py", line 297, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
                                  run_name="__mp_main__")
  File "", line 287, in run_path
  File "", line 98, in _run_module_code
  File "", line 88, in _run_code
  File "/tmp/appy_s02_07_01.py", line 39, in 
    pool = multiprocessing.Pool (n_cpu)
  File "/usr/pkg/lib/python3.14/multiprocessing/context.py", line 119, in Pool
    return Pool(processes, initializer, initargs, maxtasksperchild,
                context=self.get_context())
  File "/usr/pkg/lib/python3.14/multiprocessing/pool.py", line 215, in __init__
    self._repopulate_pool()
    ~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/pkg/lib/python3.14/multiprocessing/pool.py", line 306, in _repopulate_pool
    return self._repopulate_pool_static(self._ctx, self.Process,
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
                                        self._processes,
                                        ^^^^^^^^^^^^^^^^
    ...<3 lines>...
                                        self._maxtasksperchild,
                                        ^^^^^^^^^^^^^^^^^^^^^^^
                                        self._wrap_exception)
                                        ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/pkg/lib/python3.14/multiprocessing/pool.py", line 329, in _repopulate_pool_static
    w.start()
    ~~~~~~~^^
  File "/usr/pkg/lib/python3.14/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
                  ~~~~~~~~~~~^^^^^^
  File "/usr/pkg/lib/python3.14/multiprocessing/context.py", line 300, in _Popen
    return Popen(process_obj)
  File "/usr/pkg/lib/python3.14/multiprocessing/popen_forkserver.py", line 35, in __init__
    super().__init__(process_obj)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/usr/pkg/lib/python3.14/multiprocessing/popen_fork.py", line 20, in __init__
    self._launch(process_obj)
    ~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/usr/pkg/lib/python3.14/multiprocessing/popen_forkserver.py", line 42, in _launch
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "/usr/pkg/lib/python3.14/multiprocessing/spawn.py", line 164, in get_preparation_data
    _check_not_importing_main()
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/pkg/lib/python3.14/multiprocessing/spawn.py", line 140, in _check_not_importing_main
    raise RuntimeError('''
    ...<16 lines>...
    ''')
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

        To fix this issue, refer to the "Safe importing of main module"
        section in https://docs.python.org/3/library/multiprocessing.html
        
Traceback (most recent call last):
  File "/tmp/appy_s02_07_01.py", line 39, in 
    pool = multiprocessing.Pool (n_cpu)
  File "/usr/pkg/lib/python3.14/multiprocessing/context.py", line 119, in Pool
    return Pool(processes, initializer, initargs, maxtasksperchild,
                context=self.get_context())
  File "/usr/pkg/lib/python3.14/multiprocessing/pool.py", line 215, in __init__
    self._repopulate_pool()
    ~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/pkg/lib/python3.14/multiprocessing/pool.py", line 306, in _repopulate_pool
    return self._repopulate_pool_static(self._ctx, self.Process,
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
                                        self._processes,
                                        ^^^^^^^^^^^^^^^^
    ...<3 lines>...
                                        self._maxtasksperchild,
                                        ^^^^^^^^^^^^^^^^^^^^^^^
                                        self._wrap_exception)
                                        ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/pkg/lib/python3.14/multiprocessing/pool.py", line 329, in _repopulate_pool_static
    w.start()
    ~~~~~~~^^
  File "/usr/pkg/lib/python3.14/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
                  ~~~~~~~~~~~^^^^^^
  File "/usr/pkg/lib/python3.14/multiprocessing/context.py", line 300, in _Popen
    return Popen(process_obj)
  File "/usr/pkg/lib/python3.14/multiprocessing/popen_forkserver.py", line 35, in __init__
    super().__init__(process_obj)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/usr/pkg/lib/python3.14/multiprocessing/popen_fork.py", line 20, in __init__
    self._launch(process_obj)
    ~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/usr/pkg/lib/python3.14/multiprocessing/popen_forkserver.py", line 51, in _launch
    self.sentinel, w = forkserver.connect_to_new_process(self._fds)
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/usr/pkg/lib/python3.14/multiprocessing/forkserver.py", line 106, in connect_to_new_process
    connection.answer_challenge(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
            wrapped_client, self._forkserver_authkey)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/pkg/lib/python3.14/multiprocessing/connection.py", line 970, in answer_challenge
    message = connection.recv_bytes(256)         # reject large message
  File "/usr/pkg/lib/python3.14/multiprocessing/connection.py", line 222, in recv_bytes
    buf = self._recv_bytes(maxlength)
  File "/usr/pkg/lib/python3.14/multiprocessing/connection.py", line 447, in _recv_bytes
    buf = self._recv(4)
  File "/usr/pkg/lib/python3.14/multiprocessing/connection.py", line 416, in _recv
    raise EOFError
EOFError

https://docs.python.org/3/library/multiprocessing.html の "Safe importing of main module" の部分を見るように、との ことなので、その部分を見てみたでござる。 multiprocessing を使って実行 する関数を、 if __name__ == '__main__': の中に入れないとい けないようでござる。

fig_202603/www_python_multiprocessing_00.png

コードを以下のように変更したら、エラーが出なくなったでござる。


#!/usr/bin/env python3

#
# Time-stamp: <2026/03/03 11:32:11 (UT+08:00) daisuke>
#

# importing multiprocessing module
import multiprocessing

# number of CPUs to be used
n_cpu = 4

# output file name
file_output = 'prime_numbers_4.data'

# start and end numbers
n_start = 2
n_end   = 10**5

# function to check whether or not the number is a prime number
def is_prime_number (x):
    # resetting parameter "pn"
    pn = 1
    # examining if the number is divisible by numbers between 2 and (x-1)
    for k in range (2, x):
        # if the number is divisible by k
        if (x % k == 0):
            # then subtract 1 from "pn"
            pn -= 1
            # leaving from the loop
            break
    # returning number and a flag "pn"
    return (x, pn)

# numbers to be checked
numbers = range (n_start, n_end + 1)

# executing the function
if __name__ == '__main__':
    # making a pool of workers
    pool = multiprocessing.Pool (n_cpu)

    # executing function for different numbers in parallel
    results = pool.map (is_prime_number, numbers)

    # opening file for writing
    with open (file_output, 'w') as fh_out:
        # for each number in the list "list_pn"
        for x, pn in results:
            # writing number to the file
            if (pn):
                fh_out.write (f'{x}\n')



Frequently accessed files

  1. Misc___Taiwan/20240207_00.html
  2. Misc___Taiwan/20240819_00.html
  3. Computer___TeX/20231107_00.html
  4. Book___Chinese/20240424_00.html
  5. Computer___TeX/20230726_01.html
  6. Computer___TeX/20240411_00.html
  7. Computer___NetBSD/20250301_01.html
  8. Computer___TeX/20240414_01.html
  9. Misc___Taiwan/20240903_01.html
  10. Computer___Network/20230516_00.html
  11. Computer___Network/20241214_00.html
  12. Misc___Japan/20240718_00.html
  13. Misc___Japan/20240610_00.html
  14. Computer___NetBSD/20230119_00.html
  15. Computer___TeX/20240410_00.html
  16. Computer___Network/20240130_00.html
  17. Computer___TeX/20240414_00.html
  18. Computer___FreeBSD/20220621_0.html
  19. Computer___NetBSD/20240805_03.html
  20. Computer___NetBSD/20250307_00.html
  21. Computer___Network/20220413_1.html
  22. Computer___Python/20250330_00.html
  23. Computer___NetBSD/20220818_1.html
  24. Computer___NetBSD/20250113_00.html
  25. Computer___Python/20240101_00.html
  26. Computer___Hardware/20240820_00.html
  27. Computer___Python/20220518_0.html
  28. Computer___NetBSD/20241102_00.html
  29. Computer___TeX/20230503_00.html
  30. Computer___NetBSD/20240810_00.html


HTML file generated by Kinoshita Daisuke.