11
11
synchronization could be used.
12
12
"""
13
13
14
- from multiprocessing import Lock , Pipe , Process
14
+ import multiprocessing as mp
15
15
16
16
# lock used to ensure that two processes do not access a pipe at the same time
17
17
# NOTE This breaks testing on build runner. May work better locally
18
- # process_lock = Lock()
18
+ # process_lock = mp. Lock()
19
19
20
20
"""
21
21
The function run by the processes that sorts the list
29
29
"""
30
30
31
31
32
- def oe_process (position , value , l_send , r_send , lr_cv , rr_cv , result_pipe ):
33
- process_lock = Lock ()
32
+ def oe_process (
33
+ position ,
34
+ value ,
35
+ l_send ,
36
+ r_send ,
37
+ lr_cv ,
38
+ rr_cv ,
39
+ result_pipe ,
40
+ multiprocessing_context ,
41
+ ):
42
+ process_lock = multiprocessing_context .Lock ()
34
43
35
44
# we perform n swaps since after n swaps we know we are sorted
36
45
# we *could* stop early if we are sorted already, but it takes as long to
37
46
# find out we are sorted as it does to sort the list with this algorithm
38
47
for i in range (10 ):
39
48
if (i + position ) % 2 == 0 and r_send is not None :
40
49
# send your value to your right neighbor
41
- process_lock .acquire ()
42
- r_send [1 ].send (value )
43
- process_lock .release ()
50
+ with process_lock :
51
+ r_send [1 ].send (value )
44
52
45
53
# receive your right neighbor's value
46
- process_lock .acquire ()
47
- temp = rr_cv [0 ].recv ()
48
- process_lock .release ()
54
+ with process_lock :
55
+ temp = rr_cv [0 ].recv ()
49
56
50
57
# take the lower value since you are on the left
51
58
value = min (value , temp )
52
59
elif (i + position ) % 2 != 0 and l_send is not None :
53
60
# send your value to your left neighbor
54
- process_lock .acquire ()
55
- l_send [1 ].send (value )
56
- process_lock .release ()
61
+ with process_lock :
62
+ l_send [1 ].send (value )
57
63
58
64
# receive your left neighbor's value
59
- process_lock .acquire ()
60
- temp = lr_cv [0 ].recv ()
61
- process_lock .release ()
65
+ with process_lock :
66
+ temp = lr_cv [0 ].recv ()
62
67
63
68
# take the higher value since you are on the right
64
69
value = max (value , temp )
@@ -94,39 +99,60 @@ def odd_even_transposition(arr):
94
99
>>> odd_even_transposition(unsorted_list) == sorted(unsorted_list + [1])
95
100
False
96
101
"""
102
+ # spawn method is considered safer than fork
103
+ multiprocessing_context = mp .get_context ("spawn" )
104
+
97
105
process_array_ = []
98
106
result_pipe = []
99
107
# initialize the list of pipes where the values will be retrieved
100
108
for _ in arr :
101
- result_pipe .append (Pipe ())
109
+ result_pipe .append (multiprocessing_context . Pipe ())
102
110
# creates the processes
103
111
# the first and last process only have one neighbor so they are made outside
104
112
# of the loop
105
- temp_rs = Pipe ()
106
- temp_rr = Pipe ()
113
+ temp_rs = multiprocessing_context . Pipe ()
114
+ temp_rr = multiprocessing_context . Pipe ()
107
115
process_array_ .append (
108
- Process (
116
+ multiprocessing_context . Process (
109
117
target = oe_process ,
110
- args = (0 , arr [0 ], None , temp_rs , None , temp_rr , result_pipe [0 ]),
118
+ args = (
119
+ 0 ,
120
+ arr [0 ],
121
+ None ,
122
+ temp_rs ,
123
+ None ,
124
+ temp_rr ,
125
+ result_pipe [0 ],
126
+ multiprocessing_context ,
127
+ ),
111
128
)
112
129
)
113
130
temp_lr = temp_rs
114
131
temp_ls = temp_rr
115
132
116
133
for i in range (1 , len (arr ) - 1 ):
117
- temp_rs = Pipe ()
118
- temp_rr = Pipe ()
134
+ temp_rs = multiprocessing_context . Pipe ()
135
+ temp_rr = multiprocessing_context . Pipe ()
119
136
process_array_ .append (
120
- Process (
137
+ multiprocessing_context . Process (
121
138
target = oe_process ,
122
- args = (i , arr [i ], temp_ls , temp_rs , temp_lr , temp_rr , result_pipe [i ]),
139
+ args = (
140
+ i ,
141
+ arr [i ],
142
+ temp_ls ,
143
+ temp_rs ,
144
+ temp_lr ,
145
+ temp_rr ,
146
+ result_pipe [i ],
147
+ multiprocessing_context ,
148
+ ),
123
149
)
124
150
)
125
151
temp_lr = temp_rs
126
152
temp_ls = temp_rr
127
153
128
154
process_array_ .append (
129
- Process (
155
+ multiprocessing_context . Process (
130
156
target = oe_process ,
131
157
args = (
132
158
len (arr ) - 1 ,
@@ -136,6 +162,7 @@ def odd_even_transposition(arr):
136
162
temp_lr ,
137
163
None ,
138
164
result_pipe [len (arr ) - 1 ],
165
+ multiprocessing_context ,
139
166
),
140
167
)
141
168
)
0 commit comments