from multiprocessing import Pool import multiprocessing def f(x): current = multiprocessing.current_process() id = current.__dict__["_identity"] print("{}".format(id[-1])) return x * x if __name__ == '__main__': with Pool(2) as p: print(p.map(f, range(6)))
current process 가 가지고 있는 정보는 아래와 같다.
from multiprocessing import Pool import multiprocessing def f(x): current = multiprocessing.current_process() print('running: {}'.format(current.__dict__)) return x * x if __name__ == '__main__': with Pool(2) as p: print(p.map(f, range(6)))
running: {'_identity': (1,), '_config': {'authkey': b'(생략)', 'semprefix': '/mp', 'daemon': True}, '_parent_pid': 15516, '_popen': None, '_target': <function worker at 0x0000027F7F9AF0D0>, '_args': (<multiprocessing.queues.SimpleQueue object at 0x0000027F7F9925C0>, <multiprocessing.queues.SimpleQueue object at 0x0000027F7F9C39B0>, None, (), None, True), '_kwargs': {}, '_name': 'SpawnPoolWorker-1'} running: {'_identity': (1,), '_config': {'authkey': b'(생략)', 'semprefix': '/mp', 'daemon': True}, '_parent_pid': 15516, '_popen': None, '_target': <function worker at 0x0000027F7F9AF0D0>, '_args': (<multiprocessing.queues.SimpleQueue object at 0x0000027F7F9925C0>, <multiprocessing.queues.SimpleQueue object at 0x0000027F7F9C39B0>, None, (), None, True), '_kwargs': {}, '_name': 'SpawnPoolWorker-1'} running: {'_identity': (2,), '_config': {'authkey': b'(생략)', 'semprefix': '/mp', 'daemon': True}, '_parent_pid': 15516, '_popen': None, '_target': <function worker at 0x000001C7E31EF0D0>, '_args': (<multiprocessing.queues.SimpleQueue object at 0x000001C7E31D2978>, <multiprocessing.queues.SimpleQueue object at 0x000001C7E3203A20>, None, (), None, True), '_kwargs': {}, '_name': 'SpawnPoolWorker-2'} running: {'_identity': (1,), '_config': {'authkey': b'(생략)', 'semprefix': '/mp', 'daemon': True}, '_parent_pid': 15516, '_popen': None, '_target': <function worker at 0x0000027F7F9AF0D0>, '_args': (<multiprocessing.queues.SimpleQueue object at 0x0000027F7F9925C0>, <multiprocessing.queues.SimpleQueue object at 0x0000027F7F9C39B0>, None, (), None, True), '_kwargs': {}, '_name': 'SpawnPoolWorker-1'} running: {'_identity': (2,), '_config': {'authkey': b'(생략)', 'semprefix': '/mp', 'daemon': True}, '_parent_pid': 15516, '_popen': None, '_target': <function worker at 0x000001C7E31EF0D0>, '_args': (<multiprocessing.queues.SimpleQueue object at 0x000001C7E31D2978>, <multiprocessing.queues.SimpleQueue object at 0x000001C7E3203A20>, None, (), None, True), '_kwargs': {}, '_name': 'SpawnPoolWorker-2'} running: {'_identity': (1,), '_config': {'authkey': b'(생략)', 'semprefix': '/mp', 'daemon': True}, '_parent_pid': 15516, '_popen': None, '_target': <function worker at 0x0000027F7F9AF0D0>, '_args': (<multiprocessing.queues.SimpleQueue object at 0x0000027F7F9925C0>, <multiprocessing.queues.SimpleQueue object at 0x0000027F7F9C39B0>, None, (), None, True), '_kwargs': {}, '_name': 'SpawnPoolWorker-1'} [0, 1, 4, 9, 16, 25]