Created
November 9, 2021 09:06
-
-
Save Treebug842/aa7e9b4f000534d6a23e7697daddf44e to your computer and use it in GitHub Desktop.
Some simple concept code for multithreading and multiprocessing in python
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
# %% MultiThreading for Tasks | |
import threading | |
def multiThread(function, threadcount, *args): | |
threads = [] | |
for i in range(0, threadcount): | |
threads.append(threading.Thread(target=function, args=args)) | |
for thread in threads: | |
thread.start() | |
# Example | |
multiThread(print, 10, "Hello World") | |
# %% MultiProcessing for Speed | |
from multiprocessing import Process | |
def multiProcess(function, processCount, *args): | |
processes = [] | |
if __name__ == '__main__': | |
for i in range(0, processCount): | |
processes.append(Process(target=function, args=args)) | |
for process in processes: | |
process.start() | |
for process in processes: | |
process.join() | |
# Example | |
multiProcess(print, 10, "Hello World") | |
# %% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment