Skip to content

Instantly share code, notes, and snippets.

@Treebug842
Created November 9, 2021 09:06
Show Gist options
  • Save Treebug842/aa7e9b4f000534d6a23e7697daddf44e to your computer and use it in GitHub Desktop.
Save Treebug842/aa7e9b4f000534d6a23e7697daddf44e to your computer and use it in GitHub Desktop.
Some simple concept code for multithreading and multiprocessing in python
# %% 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