Created
May 23, 2020 14:04
-
-
Save andreaschandra/c9c4681598486d833100ebb9bd7f14e8 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
| import time | |
| import pickle | |
| import multiprocessing as mp | |
| from multiprocessing import Manager | |
| def my_worker(result): | |
| name = mp.current_process().name | |
| print(name, 'Starting') | |
| result.append(name+' Starting') | |
| time.sleep(1) | |
| print(name, 'Exiting') | |
| result.append(name+' Exiting') | |
| def my_service(result): | |
| name = mp.current_process().name | |
| print(name, 'Starting') | |
| result.append(name+' Starting') | |
| time.sleep(0.1) | |
| print(name, 'Exiting') | |
| result.append(name+' Exiting') | |
| if __name__ == '__main__': | |
| with Manager() as manager: | |
| result = manager.list() | |
| worker_list = [] | |
| for i in range(1): | |
| worker = mp.Process( | |
| name='my_worker_'+str(i), | |
| target=my_worker, | |
| args = (result,) | |
| ) | |
| service = mp.Process( | |
| name='my_service_'+str(i), | |
| target=my_service, | |
| args = (result,) | |
| ) | |
| worker_list.append(worker) | |
| worker_list.append(service) | |
| for worker_ in worker_list: | |
| worker_.start() | |
| for worker in worker_list: | |
| worker.join() | |
| print(len(result)) | |
| print(result) | |
| pickle.dump(list(result), open('result.pkl', 'wb')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment