Last active
May 2, 2025 04:58
-
-
Save maraigue/6eb87eb78b9c3f2a483f92856a02461d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing.pool import Pool | |
import multiprocessing as mp | |
def display(val): | |
print(f'{GLOBAL_VARIABLE}{val}') | |
if __name__ == '__main__': | |
GLOBAL_VARIABLE = 'test' | |
# 並列処理のプロセスの立ち上げ方をspawnにする。 | |
# この場合、Poolから呼ばれたdisplay関数でGLOBAL_VARIABLEが参照できず、エラーになる。 | |
# Windows、Macのデフォルト。 | |
mp.set_start_method('spawn') | |
# 並列処理のプロセスの立ち上げ方をforkにする。 | |
# この場合、Poolから呼ばれたdisplay関数でGLOBAL_VARIABLEが参照できる。 | |
# Macを除くPOSIXシステム(Linuxなど)のデフォルト。 | |
# Windowsでは選べない。 | |
# mp.set_start_method('fork') | |
p = Pool(4) | |
p.map(display, ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment