-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultiprocessing_demo.py
39 lines (30 loc) · 979 Bytes
/
multiprocessing_demo.py
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
"""
A simple example of using Python's multiprocessing module
Multiprocessing is basically the same as threading but memory is not shared easily
"""
import multiprocessing as mp
import random
import string
NUM_THREADS = 10
# Define an output queue
output = mp.Queue()
# Define a example function
def rand_string(length):
""" Generates a random string of numbers, lower- and uppercase chars. """
rand_str = ''.join(random.choice(
string.ascii_lowercase +
string.ascii_uppercase +
string.digits)
for i in range(length))
output.put(rand_str)
# Setup a list of processes that we want to run
processes = [mp.Process(target=rand_string, args=[5]) for x in range(NUM_THREADS)]
# Run processes
for p in processes:
p.start()
# Exit the completed processes
for p in processes:
p.join()
# Get process results from the output queue
while not output.empty():
print(output.get())