python整理-day11

一、线程

  1、基本使用

    创建线程,两种方式

    第一种:

import threading
def f1(arg):
    print(arg)

t=threading.Thread(target=f1,args=(123,))
t.start()

 结果:

123

     第二种:

import threading
class Myclass(threading.Thread):
    def __init__(self,func,args):
        self.func=func
        self.args=args
        super(Myclass,self).__init__()

    def run(self):
        self.func(self.args)

def f2(arg):
    print(arg)

obj=Myclass(f2,123)
obj.start()

 结果:

123

 第二种方法就是我们自己来创建已经类,然后调用threading.Thread这个父类,然后在使用super来进行调用

 

  2、生成者消费者模型(队列,但是和rabbitMQ不一样,这个是python自己的)

    队列:总共有四种队列,分别是先进先出队列,后进后出队列,优先级队列,双向队列

import queue

q=queue.Queue(2)

print(q.empty())

q.put(11)
q.put(22)

print(q.qsize())

print(q.get())
q.get()
q.task_done()
print(q.get())
q.get()
q.task_done()

#q.join()

 结果:

True
2
11

 这个就是普通的先进先出队列

put是放数据,是否阻塞,阻塞是的超时时间

get是取数据(默认阻塞),是否阻塞,阻塞时的超时时间

queue.Queue(2)队列最大长度

qsize()真实个数

maxsize最大支持的个数

join、task_done,阻塞进程,当队列中任务执行完毕以后,不再阻塞

 

import queue
q=queue.LifoQueue()
q.put(123)
q.put(345)
print(q.get())

q1=queue.PriorityQueue()
q1.put((1,"wzc1"))
q1.put((1,"wzc11"))
q1.put((1,"wzc1111"))
q1.put((1,"wzc111"))
q1.put((3,"wzc3"))
q1.put((2,"wzc4"))
print(q1.get())

q2=queue.deque()
q2.append(123)
q2.append(333)
q2.appendleft(456)
print(q2.pop())
print(q2.popleft())

 结果:

345
(1, 'wzc1')
333
456

 后进后出队列==lifoqueue

优先级队列==priorityqueue

双向队列==deque

 

线程锁

lock和rlock的区别,一个是只支持单层锁,一个是支持多层锁

import threading
import time

NUM=10
def func(www):
    global NUM
    www.acquire()
    NUM-=1
    time.sleep(1)
    print(NUM)
    www.release()

lock=threading.Lock()

for i in range(10):
    t=threading.Thread(target=func,args=(lock,))
    t.start()

 结果:

9
8
7
6
5
4
3
2
1
0

 

mport threading
def func(i,e):
    print(i)
    e.wait()#检测是什么信号
    print(i+100)



event=threading.Event()

for i in range(20):
    t=threading.Thread(target=func,args=(i,event,))
    t.start()

event.clear()#禁止信号
inp=input(">>>")
if inp == "1":
    event.set()#通行信号

 结果:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>>1
103
102
106
107
108
109
112
113
115
118
101
104
110
114
117
100
116
105
119
111

  另一种使用方法

import threading
def func(i,con):
    print(i)
    con.acquire()
    con.wait()
    print(i+100)
    con.release()



c=threading.Condition()

for i in range(20):
    t=threading.Thread(target=func,args=(i,c,))
    t.start()

while True:
    inp=input(">>>")
    if inp == "q":
        break
    c.acquire()
    c.notify(int(inp))
    c.release()

 结果:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>>3
>>>101
100
102
2
>>>103
104
5
>>>105
108
106
109
107

 

二、进程

  1、基本使用

  

from multiprocessing import Process
from  multiprocessing import queues
import multiprocessing
import time
def foo(i,arg):
    arg.put(i)
    print("say HI ",i,arg.qsize())


li=queues.Queue(20,ctx=multiprocessing)
for i in range(10):
    p=Process(target=foo,args=(i,li,))
 
    p.start()

 

进程测试的时候不加 if __name__ == '__main__': Windows无法执行,但是linuxmac可以,加上了就可以执行了

   基本使用

    默认数据不共享

    queue

    array

    manager.dict

    pipe

  进程池

array:

from multiprocessing import Process
from  multiprocessing import Array
import multiprocessing
import time
def foo(i,arg):
    arg[i]=i+100
    for items in arg:
        print(items)
    print("========")


li=Array("i",10)
for i in range(10):
    p=Process(target=foo,args=(i,li,))
    p.start()

 结果:

  

100
0
0
0
0
0
0
0
0
0
========
100
101
0
0
0
0
0
0
0
0
========
100
101
102
0
0
0
0
0
0
0
========
100
101
102
103
0
0
0
0
0
0
========
100
101
102
103
104
0
0
0
0
0
========
100
101
102
103
104
105
0
0
0
0
========
100
101
102
103
104
105
106
0
0
0
========
100
101
102
103
104
105
106
107
0
0
========
100
101
102
103
104
105
106
107
108
0
========
100
101
102
103
104
105
106
107
108
109
========

 

manager.dict

from multiprocessing import Process
from  multiprocessing import Manager
import multiprocessing
import time
def foo(i,arg):
    arg[i]=i+100
    print(arg.values())

obj=Manager()
li=obj.dict()
for i in range(10):
    p=Process(target=foo,args=(i,li,))
    p.start()
time.sleep(0.1)

 结果:

[100]
[100, 101]
[100, 101, 102]
[100, 101, 102, 103]
[100, 101, 102, 103, 104]
[100, 101, 102, 103, 104, 105]
[100, 101, 102, 103, 104, 105, 106]
[100, 101, 102, 103, 104, 105, 106, 107]
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109]
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109]

 

进程池

pool.close()所有的任务执行完毕

pool.terminal()立即终止

from multiprocessing import Pool
import time


def f1(arg):
    time.sleep(1)
    print(arg)




pool=Pool(5)

for i in range(30):
    #pool.apply(func=f1,args=(i,))
    pool.apply_async(func=f1,args=(i,))

pool.close()
#time.sleep(1)
#pool.terminate()
pool.join()

 结果:

0
1
2
3
4
5
6
7
8
9
11
10
12
13
14
16
15
17
18
19
21
22
20
23
24
25
27
26
28
29

 三、协程

  1、什么时候使用协程

  2、greenlet

  3、gevent

greenlet:

from greenlet import greenlet

def test1():
    print(12)
    gr2.swith()
    print(34)
    gr2.swith()

def test2():
    print(56)
    gr1.swith()
    print(78)

gr1=greenlet(test1)
gr2=greenlet(test2)

gr1.swith()

 

gevent:

from gevent import monkey;monkey.patch.all()
import gevent
import requests

def f(url):
    print("GET : %s" % url)
    resp=requests.get(url)
    data=resp.text
    print("%d bytes received from %s" % (len(data),url))

gevent.joinall([
    gevent.spwan(f,'https://www.baidu.con'),
    gevent.spwan(f,'https://www.yahoo.con'),
    gevent.spwan(f,'https://www.github.con')

])

 

posted @ 2016-07-23 10:43  wzc1206  阅读(153)  评论(0编辑  收藏  举报