Skip to content

Instantly share code, notes, and snippets.

@Kvanrooyen
Created May 22, 2019 20:40
Show Gist options
  • Save Kvanrooyen/faa0f409824a549d73f22fcffb225ac8 to your computer and use it in GitHub Desktop.
Save Kvanrooyen/faa0f409824a549d73f22fcffb225ac8 to your computer and use it in GitHub Desktop.
Multi processing in Python
# Testing of how multi processing works
# https://www.youtube.com/watch?v=ecKWiaHCEKs
"""
Multiprocessing:
- A new process is started independent from the first process
- Starting a process is slower than starting a thread
- Memory is not shared between processes
- Mutexes not necessary (unless threading in the new process)
- One GIL (Global Interpreter Lock) for each process
"""
# This makes all cpu cores run at 100%
from multiprocessing import Process
import os
import math
def calc(num):
for i in range(0, 7000000):
math.sqrt(i)
def main():
processes = []
for i in range(os.cpu_count()):
print('Process: %d' % i)
processes.append(Process(target=calc, args=(46560546845,)))
for process in processes:
process.start()
for process in processes:
process.join()
print('\n\nDone!')
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment