Last active
December 7, 2017 13:46
-
-
Save developer-sdk/9ec6d9d882b412b729858f0f81cdddc1 to your computer and use it in GitHub Desktop.
python MulitProcessing 처리 방법 3가지 예제
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
| #!/usr/bin/python | |
| # -*- coding: utf-8 -*- | |
| import multiprocessing | |
| import random | |
| import time | |
| def worker(num): | |
| for i in range(5): | |
| print("{0}, {1}".format(num, i)) | |
| return | |
| if __name__ == '__main__': | |
| jobs = [] | |
| for i in range(5): | |
| p = multiprocessing.Process(target=worker, args=(i,)) | |
| jobs.append(p) | |
| p.start() | |
| # Iterate through the list of jobs and remove one that are finished, checking every second. | |
| for job in jobs: | |
| job.join() | |
| print('*** All jobs finished ***') |
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
| #!/usr/bin/python | |
| # -*- coding: utf-8 -*- | |
| from multiprocessing import Pool | |
| import random, time | |
| def worker(num): | |
| for i in range(5): | |
| print("{0}, {1}".format(num, i)) | |
| return | |
| if __name__ == '__main__': | |
| pool = Pool(processes=4) | |
| pool.map(worker, range(5)) | |
| print('*** All jobs finished ***') |
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
| #!/usr/bin/python | |
| # -*- coding: utf-8 -*- | |
| from multiprocessing import Pool | |
| import random, time | |
| def worker(num): | |
| for i in range(5): | |
| print("{0}, {1}".format(num, i)) | |
| return | |
| if __name__ == '__main__': | |
| with Pool(processes=4) as pool: | |
| pool.map(worker, range(5)) | |
| pool.join() | |
| print('*** All jobs finished ***') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment