[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/Lovepython015/Python/master/Article/PythonBasis/python13/2.md [Back]  [Original]

#  #



* New 
* Runnable 
* Running 
* Blocked  Wait Locked Sleeping
* Dead 



* 
* 
* 
* 



## 1 ##

Python  `thread`  `threading`



 `threading` 

```python
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import time
import threading


class MyThread(threading.Thread):
    def run(self):
        for i in range(5):
            print('thread {}, @number: {}'.format(self.name, i))
            time.sleep(1)


def main():
    print("Start main threading")

    # 
    threads = [MyThread() for i in range(3)]
    # 
    for t in threads:
        t.start()

    print("End Main threading")


if __name__ == '__main__':
    main()

```



```txt
Start main threading
thread Thread-1, @number: 0
thread Thread-2, @number: 0
thread Thread-3, @number: 0
End Main threading
thread Thread-2, @number: 1
thread Thread-1, @number: 1
thread Thread-3, @number: 1
thread Thread-1, @number: 2
thread Thread-3, @number: 2
thread Thread-2, @number: 2
thread Thread-2, @number: 3
thread Thread-3, @number: 3
thread Thread-1, @number: 3
thread Thread-3, @number: 4
thread Thread-2, @number: 4
thread Thread-1, @number: 4
```



## 2join ##



 `join` 



```python
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import time
import threading


class MyThread(threading.Thread):
    def run(self):
        for i in range(5):
            print('thread {}, @number: {}'.format(self.name, i))
            time.sleep(1)


def main():
    print("Start main threading")

    # 
    threads = [MyThread() for i in range(3)]
    # 
    for t in threads:
        t.start()

    #  join
    for t in threads:
        t.join()

    print("End Main threading")


if __name__ == '__main__':
    main()

```




```txt
Start main threading
thread Thread-1, @number: 0
thread Thread-2, @number: 0
thread Thread-3, @number: 0
thread Thread-1, @number: 1
thread Thread-3, @number: 1
thread Thread-2, @number: 1
thread Thread-2, @number: 2
thread Thread-1, @number: 2
thread Thread-3, @number: 2
thread Thread-2, @number: 3
thread Thread-1, @number: 3
thread Thread-3, @number: 3
thread Thread-3, @number: 4
thread Thread-2, @number: 4
thread Thread-1, @number: 4
End Main threading

```
## 3 ##



 `threading`  Lock 

```python
lock = threading.Lock()
```



```python
lock.acquire()
```



```python
lock.release()
```

Python RLockRLock  Lock  counter counter  acquire  require acquire  release



```python
r_lock = threading.RLock()
```

## 4Condition  ##



Python  Condition 

** Condition Condition  Lock  acquire  release  wait  notify **

 acquire  wait notify  wait 

 acquire  Lock  RLock  waiting  notify 

![](http://twowaterimage.oss-cn-beijing.aliyuncs.com/2019-10-14-Condition.png)



```python
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import threading, time


class Consumer(threading.Thread):
    def __init__(self, cond, name):
        # 
        super(Consumer, self).__init__()
        self.cond = cond
        self.name = name

    def run(self):
        # Seeker
        time.sleep(1)
        self.cond.acquire()
        print(self.name + ': ')
        self.cond.notify()
        self.cond.wait()
        print(self.name + ': ')
        self.cond.notify()
        self.cond.wait()
        print(self.name + ': ')
        self.cond.notify()
        self.cond.release()
        print(self.name + ': ')


class Producer(threading.Thread):
    def __init__(self, cond, name):
        super(Producer, self).__init__()
        self.cond = cond
        self.name = name

    def run(self):
        self.cond.acquire()
        #  notify 
        self.cond.wait()
        print(self.name + ': ')
        self.cond.notify()
        self.cond.wait()
        print(self.name + ': ')
        self.cond.notify()
        self.cond.wait()
        print(self.name + ': ')
        self.cond.release()
        print(self.name + ': ')


cond = threading.Condition()
consumer = Consumer(cond, '')
producer = Producer(cond, '')
consumer.start()
producer.start()

```



```txt
: 
: 
: 
: 
: 
: 
: 
: 
```

## 5 ##



 queue  `Queue`  `put()`  `get()` 

```python
# -*- coding: UTF-8 -*-
from queue import Queue
from threading import Thread

isRead = True


def write(q):
    # 
    for value in ['', '', '']:
        print(' Queue {0}'.format(value))
        q.put(value)


def read(q):
    # 
    while isRead:
        value = q.get(True)
        print(' Queue {0}'.format(value))


if __name__ == '__main__':
    q = Queue()
    t1 = Thread(target=write, args=(q,))
    t2 = Thread(target=read, args=(q,))
    t1.start()
    t2.start()
```



```txt
 Queue 
 Queue 
 Queue 
 Queue 
 Queue 
 Queue 
```

Python  Event 

Event 

* 

 Event  `set()`  Event Event  `isSe()`  event  `set()` `isSet()` 

* 

 Event  `clear()`  Event  Event  clear isSet() 

*  

Event  wait  Event  wait 



```python
# -*- coding: UTF-8 -*-

import threading


class mThread(threading.Thread):
    def __init__(self, threadname):
        threading.Thread.__init__(self, name=threadname)

    def run(self):
        # Event
        global event
        # Event
        if event.isSet():
            event.clear()
            event.wait()
            print(self.getName())
        else:
            print(self.getName())
            # Event
            event.set()

# Event
event = threading.Event()
# Event
event.set()
t1 = []
for i in range(10):
    t = mThread(str(i))
    # 
    t1.append(t)

for i in t1:
    # 
    i.start()

```




```txt
1
0
3
2
5
4
7
6
9
8
```


## 6 ##

 joinPython  `setDeamon` 



Web Proxy Viewer  |  New URL  |  Original Page