Python multi-threaded programming, using the Semaphore control thread

xiaoxiao2021-03-06  67

#! / Usr / bin / env python

IMPORT OS

Import Time

Import threading

# Process function

Def PrintThread (N):

Print N,

"-> process creation"

For a

In Range (4):

Print A

Time.sleep (1)

Print N,

"-> process end"

Sem.release ()

IF __NAME__ == '__ main__':

Maxthread = 5

#

SEM = threading.boundedsemaphore (Maxthread)

For a

In Range (12):

Sem.acquire ()

Threading.Thread (Target = Printthread, Args = (a,)). START ()

Print

"All Thread Has Create, Wait for All Thread EXIT."

For a

In RanThread:

Sem.acquire ();

Print

"All thread exit"

转载请注明原文地址:https://www.9cbs.com/read-108382.html

New Post(0)