Multiprocess + Semaphore Semaphore

<div class="iteye-blog-content-contain" style="font-size: 14px"><p><div id="cnblogs_post_body"></p>
<p> <p>通过下面一个例子进行理解。</p> </p>
<p> <div class="cnblogs_code"> </p>
<p>  <pre><span style="color: #0000ff">from</span> multiprocessing <span style="color: #0000ff">import</span><span style="color: #000000"> Process</p>
<p></span><span style="color: #0000ff">from</span> multiprocessing <span style="color: #0000ff">import</span><span style="color: #000000"> Semaphore</p>
<p></span><span style="color: #0000ff">import</span><span style="color: #000000"> datetime</p>
<p></span><span style="color: #0000ff">import</span><span style="color: #000000"> time</p>
<p></span><span style="color: #0000ff">import</span><span style="color: #000000"> multiprocessing</p>
<p> </p>
<p></span><span style="color: #0000ff">def</span><span style="color: #000000"> worker(s, i):</p>
<p>    s.acquire()</p>
<p>    </span><span style="color: #0000ff">print</span>(multiprocessing.current_process().name + <span style="color: #800000">&quot;</span><span style="color: #800000"> acquire</span><span style="color: #800000">&quot;</span><span style="color: #000000">,datetime.datetime.now())</p>
<p>    time.sleep(i)</p>
<p>    </span><span style="color: #0000ff">print</span>(multiprocessing.current_process().name + <span style="color: #800000">&quot;</span><span style="color: #800000"> release</span><span style="color: #800000">&quot;</span>,datetime.datetime.now(),<span style="color: #800000">&quot;</span><span style="color: #800000">\n</span><span style="color: #800000">&quot;</span><span style="color: #000000">)</p>
<p>    s.release()</p>
<p> </p>
<p></span><span style="color: #0000ff">if</span> <span style="color: #800080">__name__</span> == <span style="color: #800000">&quot;</span><span style="color: #800000">__main__</span><span style="color: #800000">&quot;</span><span style="color: #000000">:</p>
<p>    s </span>= multiprocessing.Semaphore(2<span style="color: #000000">)</p>
<p>    </span><span style="color: #0000ff">for</span> i <span style="color:#0000ff">in</span> range(5<span style="color: #000000">):</p>
<p>        p </span>= multiprocessing.Process(target = worker, args=(s, i*2<span style="color: #000000">))</p>
<p>        p.start()</span></pre> </p>
<p> </div> </p>
<p> <p>运行结果:</p> </p>
<p> <div style="white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size: 14px"></p>
<p>  <img alt="" data-media-type="image" data-attr-org-src-id="864F8CD1AAAB4237A6FEBE704AE959E2" /></p>
<p>  <img src="http://images2017.cnblogs.com/blog/1255548/201711/1255548-20171120232934790-2105426550.png" alt="" /></p>
<p> </div> </p>
<p> <div style="white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size: 14px; text-align: left"></p>
<p>  分析:</p>
<p> </div> </p>
<p> <div style="white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size: 14px"></p>
<p>  < span style="font-size: 12px">p = multiprocessing.Process(……) defines five processes, p.start five processes in parallel, resulting in the result of the semaphore. </span></p>
<p> </div> </p>
<p> <div> </p>
<p>  <div style= "white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size: 14px"></p>
<p>   <span style="font-size: 12px">s = multiprocessing.Semaphore(2) defines a maximum semaphore of 2, release: +1 acquire: -1</span></p>


<p>   <span style="font-size: 12px">22:41:30</span></p>
<p>  </div> </p>
<p>  <div style="white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size: 14px"></p>
<p>   <span style="font-size: 12px">第一步,五个进程并发执行,进程1执行并等待0s,S-1=1</span></p>
<p>  </div> </p>
<p>  <div style="white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size: 14px"></p>
<p>   <span style="font-size: 12px">In the second step, five processes are executed concurrently, process 2 executes and waits for 2s, S-1=0</span></p> <p>  ; <div style="white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size: 14px"></p>
<p>  </div> </p>

<p>   <span style="font-size: 12px"> The third step, because process 1 is executed, and the waiting time is 0, and process 2 needs to wait 2s. So this step must be the execution of process 1, and the execution of process 1 is completed, and the semaphore +1 and enters non-blocking. </span></p>
<p>  </div> </p>
<p>  <div style="white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size: 14px"></p>
<p>   <span style="font-size: 12px"> Step 4, process 2 enters and waits for 2s, so Only three processes 3, 4, and 5 are left in parallel, process 4 executes and waits for 6s, S-1=0</span></p>
<p>  </div> </p>
< p>  <div style="white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size: 14px"></p>
<p>

<p>  <div style="white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size: 14px"></p>
<p>    <span style="font-size: 12px">Step 5, process 2 waits for 2s to complete, process 2 executes, S+1=1</span></p>
<p>  </div> ; </p>
<p>  <div style="white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size: 14px"></p>
<p>   <span style="font-size: 12px"> Step 6, process 4 is waiting, only two processes 3 and 5 are left in parallel, process 3 executes and waits for 4s, S- 1=0</span></p>
<p>  </div> </p>
<p>  <div style="white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size: 14px"></p>
<p>   <span style="font-size: 12px"><span style="font-size: 12px">22:41:36</span></span></p>
<p>  </div> </p>
<p>  <div style="white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size : 14px"></p>
<p>   <span style="font-size: 12px"> Step 7, process 4 is waiting for 6s, process 3 is 4s, so process 3 executes , S+1=1</span></p>
<p>  </div> </p>
<p>  <div style="white-space: pre-wrap; text -indent: 56px; line-height: 1.75; font-size: 14px"></p>
<p>   <span style="font-size: 12px">Step 8, Process 4 Wait, process 5 executes and waits for 8s, S-1=0</span></p>
<p>  </div> </p>
< p>  <div style="white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size: 14px"></p>
<p>   <span style="font-size: 12px">第九步,进程4执行,S+1=1</span></p>
<p>  </div> </p>
<p>  <div style="white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size: 14px"></p>
<p>   <span style="font-size: 12px"><span style="font-size: 12px">22:41:44</span></span></p>
<p>  </div> </p>
<p>  <div style="white-space: pre-wrap; text-indent: 56px; line-height: 1.75; font-size: 14px"></p>
<p>   <span style="font-size: 12px">第十步,进程5执行,S-1=0</span></p>
<p>  </div> </p>
<p> </div> </p>
<p> <div></p>
<p>  <img alt="" data-media-type="image" data-attr-org-src-id="864F8CD1AAAB4237A6FEBE704AE959E2" /></p>
<p> </div></p>
<p></div></p></div>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326413958&siteId=291194637