Skip to content

Instantly share code, notes, and snippets.

@thomasbrandon
Created October 14, 2019 05:40
Show Gist options
  • Save thomasbrandon/12359dda60cb244cd1b7841107b095ef to your computer and use it in GitHub Desktop.
Save thomasbrandon/12359dda60cb244cd1b7841107b095ef to your computer and use it in GitHub Desktop.
Multiprocessing Test
#/usr/bin/env python
from fastprogress.fastprogress import master_bar, progress_bar
from IPython import get_ipython
from time import sleep
import torch
import sys
def mp_fn(index):
print(f"Proc{index}: Shell name: " + get_ipython().__class__.__name__ + "\n")
pbar = progress_bar(range(10))
pbar.prefix = f'Proc{index}'
for _ in pbar: sleep(0.3)
if __name__ == '__main__':
print("Master: Shell name: " + get_ipython().__class__.__name__ + "\n")
main_mod = sys.modules['__main__']
print("Main mod dunders: " + ', '.join([n for n in dir(main_mod) if n.startswith('__')]))
print(f"main_mod __spec__: {getattr(main_mod, '__spec__', None)}")
# This is the code multiprocessing does to spawn:
main_mod_name = getattr(main_mod.__spec__, "name", None)
if main_mod_name is not None: print("No name for main module")
pbar = progress_bar(range(10))
pbar.prefix = 'Master'
for _ in pbar: sleep(0.3)
print("Launching subprocesses...")
res = torch.multiprocessing.spawn(mp_fn, nprocs=4, join=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment