-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiprocessing_example.py
More file actions
42 lines (30 loc) · 1008 Bytes
/
multiprocessing_example.py
File metadata and controls
42 lines (30 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Intro to multiprocessing
# multiple processe running the same script
# this is very different from threading
# Each process has its own memory space, and its own threads
import logging
import multiprocessing
from multiprocessing import process
import time
# Process starting function
def run(num):
name = process.current_process().name
logging.info(f'Running {name} as {__name__}')
time.sleep(num * 2)
logging.info(f'Finished {name}')
# Basic process usage
def main():
logging.info('Starting')
name = process.current_process().name
logging.info(f' Running {name} as {__name__}')
processes = []
for x in range(5):
p = multiprocessing.Process(target=run, args=[x], daemon=True)
processes.append(p)
p.start()
for p in processes:
p.join()
logging.info(f'Finished {name}')
logging.basicConfig(format='%(levelname)s - %(asctime)s: %(message)s', datefmt='%H:%M:%S', level=logging.DEBUG)
if __name__ == "__main__":
main()