二. 线程:
enumerate : 返回一个存储着所有线程对象的列表
from threading import enumerate,Thread
import time
def func():
time.sleep(0.5) #没有这句,线程执行太快,会捕捉不到,enumerate显示的是还活着的线程
print('in son thread')
Thread(target = func).start()
Thread(target = func).start()
print(enumerate())
守护线程:
如果没有守护线程,子线程有时候成为死循环,主线程也不会结束,进程就结束不了
守护线程会和其他子线程一样,主线程执行完了任务,进程把守护线程结束
主进程 有一个 主线程 -- 有一堆子线程
import time
from threading import Thread
def daemon_func():
while True:
time.sleep(0.1)
print('守护线程')
def son_func():
print('start son')
time.sleep(1)
print('end son')
t = Thread(target = daemon_func)
t.daemon = True #开启守护线程
t.start() #这里t就是守护线程了
Thread(target = son_func).start()
time.sleep(1)
print('主线程结束')
队列:
1,Queue 就是一个线程队列的类,自带lock锁,实现了线程安全的数据类型
2,LifoQueue 先进后出的队列
3. PriorityQueue 优先级队列
from queue import Queue
q = Queue() # 注意这里要加括号
q.put({1, 2, 3})
q.put_nowait('abc')
print(q.get_nowait())
print(q.get())
例子2:
from queue import LifoQueue
lfq = LifoQueue()
lfq.put(1)
lfq.put('abc')
lfq.put({'1','2'})
print(lfq.get())
print(lfq.get())
print(lfq.get())
print(lfq.get())
例子3:
# pq = PriorityQueue()
# pq.put((10,'askdhiu'))
# pq.put((2,'asljlg'))
# pq.put((20,'asljlg'))
# print(pq.get())
# print(pq.get())
# print(pq.get())
#用线程写一个生产者和消费者
from threading import Thread
from queue import Queue
import time
#生产者
def Maker(name,q):
for i in range(10):
time.sleep(0.3)
print('生产了食物%s'%i)
food = '生产了食物%s'%i
q.put(food)
# 消费者
def Course(name,q):
while True:
q.get()
print('%s消费了食物'%(name))
if __name__ == '__main__':
q = Queue()
p1 = Thread(target=Maker, args=('alex', q))
p1.start()
p2 = Thread(target=Course, args=('wusir', q))
p2.start()
4. gevent 模块,协程,协程就是开启一堆线程,遇到阻塞,就执行下面的线程
import time
import gevent
def eat():
print('start eating')
gevent.sleep(1)
print('end eating')
def sleep():
print('start sleeping')
gevent.sleep(1)
print('end sleeping')
g1 = gevent.spawn(eat)
g2 = gevent.spawn(sleep)
time.sleep(3)
g1.join()
g2.join()
#gevent.joinall([g1, g2])
修改2
from gevent import monkey
monkey.patch_all()
import time
import gevent
def eat():
print('start eating')
time.sleep(1)
print('end eating')
def sleep():
print('start sleeping')
time.sleep(1)
print('end sleeping')
g1 = gevent.spawn(eat)
g2 = gevent.spawn(sleep)
gevent.joinall([g1, g2])
time.sleep(14)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2019/2/17 14:06
# @Author : wuchao
# @Site :
# @File : test.py
# @Software: PyCharm
import socket
from threading import Thread
#注意一点,不开多线程完全是可以搞定的,在这里只是教大家要有并发编程的思想,所以我使用了多线程
sk = socket.socket()
sk.bind(('127.0.0.1',8001))
sk.listen()
def func1(conn):
with open('test.html', 'rb') as f:
# with open('Python开发.html','rb') as f:
data = f.read()
conn.send(data)
conn.close()
def func2(conn):
with open('meinv.png', 'rb') as f:
pic_data = f.read()
# conn.send(b'HTTP/1.1 200 ok \r\n\r\n')
conn.send(pic_data)
conn.close()
def func3(conn):
with open('test.css', 'rb') as f:
css_data = f.read()
conn.send(css_data)
conn.close()
def func4(conn):
with open('wechat.ico', 'rb') as f:
ico_data = f.read()
conn.send(ico_data)
conn.close()
def func5(conn):
with open('test.js', 'rb') as f:
js_data = f.read()
conn.send(js_data)
conn.close()
while 1:
conn,addr = sk.accept()
# while 1:
from_b_msg = conn.recv(1024)
str_msg = from_b_msg.decode('utf-8')
path = str_msg.split('\r\n')[0].split(' ')[1]
print('path>>>',path)
conn.send(b'HTTP/1.1 200 ok \r\n\r\n')
print(from_b_msg)
if path == '/':
#这里开启多线程是指有三个人来访问访问根目录,那么第一个人来了就给第一个人开一个线程,然后就不管了,这时候第二个人来了,就给第二个人开一个线程,不用等待第一个人结束在开启
# func1(conn)
t = Thread(target=func1,args=(conn,))
t.start()
elif path == '/meinv.png':
# func2(conn)
t = Thread(target=func2, args=(conn,))
t.start()
elif path == '/test.css':
# func3(conn)
t = Thread(target=func3, args=(conn,))
t.start()
elif path == '/wechat.ico':
# func4(conn)
t = Thread(target=func4, args=(conn,))
t.start()
elif path == '/test.js':
# func5(conn)
t = Thread(target=func5, args=(conn,))
t.start()