Last active
September 5, 2020 16:03
-
-
Save arashmad/d3018a7f5112250d52170cdd369285c5 to your computer and use it in GitHub Desktop.
Helps you to do a repetitive procedure by distributing it across all CPU's cores
This file contains 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 random | |
from multiprocessing import Process | |
workers = [] | |
def do_something(a:str, b:int): | |
""" | |
This is your main function that you want to be | |
execute "n" times but much faster than usual by | |
using all youe machine resources. It is sent to | |
all cores of CPU using "multiprocessing". This | |
can be compared with following loop: | |
>>> for i in range(0,20): | |
>>> do_something(10,20) | |
""" | |
time.sleep(3) | |
print(random.random()*a + b) | |
def main(n): | |
if __name__ == '__main__': | |
for i in range(0,n): | |
p = Process(target=do_something, args=(10, 20)) | |
workers.append(p) | |
p.start() | |
for t in workers: | |
t.join() | |
main(20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment