路飞学城Python-Day32【小结】
import socket from multiprocessing import Process def talk(conn): while True: try: data = conn.recv(1024) if not data:break conn.send(data.upper()) except ConnectionError: break conn.close() def server(): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server.bind(('127.0.0.1', 8080)) server.listen(5) while True: conn, addr = server.accept() p = Process(target=talk, args=(conn,)) p.start() server.close() if __name__ == '__main__': server()
import socket client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect(('127.0.0.1', 8080)) while True: msg = input('>>>>').strip() if not msg:continue client.send(msg.encode('gbk')) data = client.recv(1024) print(data.decode('gbk'))
from multiprocessing import Process import time import random def task(n): time.sleep(random.randint(1,3)) print('-------->%s' % n) if __name__ == '__main__': p1=Process(target=task,args=(1,)) p2=Process(target=task,args=(2,)) p3=Process(target=task,args=(3,)) p1.start() p2.start() p3.start() p1.join() p2.join() p3.join() print('-------->4') # 效果二:保证最后输出-------->4 # # -------->2 # -------->3 # -------->1 # -------->4
import socket client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect(('127.0.0.1', 8080)) while True: msg = input('>>>').strip() if not msg:break client.send(msg.encode('gbk')) data = client.recv(1024) print(data.decode('gbk')) client.close()
import socket from threading import Thread def server(ip, port): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((ip, port)) server.listen(5) while True: conn, addr = server.accept() t = Thread(target=comm, args=(conn,)) t.start() server.close() def comm(conn): while True: try: data = conn.recv(1024) if not data:break conn.send(data.upper()) except ConnectionError: break conn.close() if __name__ == '__main__': server('127.0.0.1', 8080)
import socket from threading import Thread,currentThread def client(): client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect(('127.0.0.1', 8080)) while True: client.send(('%s hello'%currentThread().getName()).encode('gbk')) data = client.recv(1024) print(data.decode('gbk')) client.close() if __name__ == '__main__': for i in range(500): t = Thread(target=client) t.start()
import socket from gevent import monkey,spawn;monkey.patch_all() def comm(conn): while True: try: data = conn.recv(1024) if not data:break conn.send(data.upper()) except ConnectionResetError: break conn.close() def server(ip,port): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((ip, port)) server.listen(5) while True: conn, addr = server.accept() spawn(comm, conn) server.close() if __name__ == '__main__': g = spawn(server('127.0.0.1', 8080)) g.join()
import socket from threading import Thread,currentThread def client(): client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect(('127.0.0.1', 8080)) while True: client.send(('%s hello'%currentThread().getName()).encode('gbk')) data = client.recv(1024) print(data.decode('gbk')) client.close() if __name__ == '__main__': for i in range(1): t = Thread(target=client) t.start()
import socket server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(('127.0.0.1',8080)) server.listen(5) server.setblocking(False) print('starting.....') rlist = [] while True: try: conn, addr = server.accept() rlist.append(conn) except BlockingIOError: del_rlist = [] for conn in rlist: try: data = conn.recv(1024) if not data: del_rlist.append(conn) continue except BlockingIOError: continue except Exception: conn.close() del_rlist.append(conn) for conn in del_rlist: rlist.remove(conn) # while True: # try: # data = conn.recv(1024) # if not data:break # conn.send(data.upper()) # except ConnectionResetError: # break # conn.close() server.close()
# 开启子进程的目的是执行一个任务 # 方式一 from multiprocessing import Process import time def task(name): print('%s is running' % name) time.sleep(4) print('%s is done' % name) # windows操作系统一定要在main下执行Process if __name__ == '__main__': p1 = Process(target=task, args=('panda',)) #实例化一个对象 p2 = Process(target=task, kwargs={'name':'boy'}) #实例化一个对象 p1.start() #仅仅是给操作系统发送了一个信号 p2.start() print('主进程结束') # -------------------------------------------------------------------------- # 方式二 # 改写默认的一个类,提供功能 from multiprocessing import Process import time class MyProcess(Process): def __init__(self, name): super().__init__() self.name = name def run(self): print('%s is running' % self.name) time.sleep(4) print('%s is done' % self.name) if __name__ == '__main__': p1 = MyProcess('panda') p1.start() print('主进程已经结束了')
# join方法 # 如果主进程需要等待子进程结束以后才能运行,就需要join方法 # from multiprocessing import Process # import time # import os # def task(): # print('%s is running.Parent is is %s' % (os.getpid(),os.getppid())) # time.sleep(4) # print('%s is done.Parent is is %s' % (os.getpid(),os.getppid())) # # windows操作系统一定要在main下执行Process # if __name__ == '__main__': # p1 = Process(target=task) #实例化一个对象 # p2 = Process(target=task) #实例化一个对象 # p1.start() #仅仅是给操作系统发送了一个信号 # p2.start() # p1.join() # p2.join() # print('主进程结束',os.getpid(),os.getppid()) # print(p1.pid) ######################################################################## # from multiprocessing import Process # import time # import os # def task(name, n): # print('%s is running' % name) # time.sleep(n) # # # windows操作系统一定要在main下执行Process # if __name__ == '__main__': # start = time.time() # p1 = Process(target=task, args=('子进程1',1)) #实例化一个对象 # p2 = Process(target=task, args=('子进程2',3)) #实例化一个对象 # p3 = Process(target=task, args=('子进程3',5)) # p1.start() #仅仅是给操作系统发送了一个信号,是由操作系统去调度是否启动线程,执行顺序是无法确定的! # p2.start() # p3.start() # p1.join() # p2.join() # p3.join() # print('主进程结束',(time.time()-start)) ######################################################################## from multiprocessing import Process import time import os def task(name, n): print('%s is running' % name) time.sleep(n) # windows操作系统一定要在main下执行Process if __name__ == '__main__': start = time.time() p1 = Process(target=task, args=('子进程1',1), name='panda') #实例化一个对象 p2 = Process(target=task, args=('子进程2',3)) #实例化一个对象 p3 = Process(target=task, args=('子进程3',5)) p1.start() #仅仅是给操作系统发送了一个信号,是由操作系统去调度是否启动线程,执行顺序是无法确定的! p1.join() print(p2.is_alive()) p2.start() print(p2.is_alive()) p2.join() p3.start() p3.join() print('主进程结束',(time.time()-start)) print(p1.name)
# from multiprocessing import Process # import time # # # def task(name): # print('%s in running' % name) # time.sleep(2) # # if __name__ == '__main__': # p = Process(target=task, args=(('子进程1',))) # # 守护进程的设置一定是要在子进程开始之前设置守护进程 # p.daemon = True # p.start() # # print('主进程结束') #--------------------------------------------------- # 练习题 from multiprocessing import Process import time def foo(): print('123') time.sleep(1) print('end123') def bar(): print('456') time.sleep(3) print('end456') if __name__ == '__main__': p1 = Process(target=foo) p2 = Process(target=bar) p1.daemon = True p1.start() p2.start() time.sleep(0.5) print('主进程结束')
# 抢票的环节 # 1.先去看网站的票数 # 2.假设票数如果为0就不能再买票了 import time import json from multiprocessing import Process, Lock def search(name): time.sleep(1) #模拟网络延迟 dic = json.load(open('db.txt', 'r', encoding='utf-8')) print('<%s> 查看剩余的票数[%s]' % (name, dic['count'])) def get(name): time.sleep(1) dic = json.load(open('db.txt', 'r', encoding='utf-8')) if dic['count'] > 0: dic['count'] -= 1 time.sleep(3) json.dump(dic, open('db.txt', 'w', encoding='utf-8')) print('<%s>购票成功!' % name) def task(name, mutex): search(name) mutex.acquire() get(name) mutex.release() if __name__ == '__main__': mutex = Lock() for i in range(10): p = Process(target=task, args=('路人%s' % i, mutex)) p.start()
# 抢票的环节 # 1.先去看网站的票数 # 2.假设票数如果为0就不能再买票了 import time import json from multiprocessing import Process, Lock def search(name): time.sleep(1) #模拟网络延迟 dic = json.load(open('db.txt', 'r', encoding='utf-8')) print('<%s> 查看剩余的票数[%s]' % (name, dic['count'])) def get(name): time.sleep(1) dic = json.load(open('db.txt', 'r', encoding='utf-8')) if dic['count'] > 0: dic['count'] -= 1 time.sleep(3) json.dump(dic, open('db.txt', 'w', encoding='utf-8')) print('<%s>购票成功!' % name) else: print('<%s>购票失败!'%name) def task(name, ): search(name) # mutex.acquire() get(name) # mutex.release() if __name__ == '__main__': # mutex = Lock() for i in range(10): p = Process(target=task, args=('路人%s' % i, )) p.start() p.join()
from multiprocessing import Queue # 队列里不应该放大的文件数据,一般发送的只是精简的小数据 # 队列确实可以不指定大小,但是队列的数据受限于内存的大小 # 队列会自动加锁 # 队列是多个进程之间通信使用的 q = Queue(3) q.put('hello') q.put({'a':1}) q.put([1,1,1,1]) # 查看队列是否满了 # print(q.full()) # 先进先出 print(q.get()) print(q.get()) print(q.get()) # print(q.empty())
from multiprocessing import Queue # 队列里不应该放大的文件数据,一般发送的只是精简的小数据 # 队列确实可以不指定大小,但是队列的数据受限于内存的大小 # 队列会自动加锁 # 队列是多个进程之间通信使用的 q = Queue(3) q.put('hello') q.put({'a':1}) q.put([1,1,1,1]) # 查看队列是否满了 # print(q.full()) # 先进先出 print(q.get()) print(q.get()) print(q.get()) # print(q.empty())
from multiprocessing import Process,Queue import time # 生产者在消费者消费的同时不能生产,消费者在生产的同时不能消费 # 这样的问题会导致生产者和消费者之间彼此在互相等待 # 生产者消费者模型就是可以解决这样的问题, # 在生产者和消费者之间建立一个容器,生产者往容器里生产,消费者去容器里取,这样就解决了耦合问题 # 好处1:解耦合 # 好处2:平衡了数据处理的时间差 # 基于队列实现生产者消费者模型 def producer(q): for i in range(10): res = '制造品%s' % i time.sleep(0.5) print('生产者生产了%s' % res) q.put(res) def consumer(q): while True: res = q.get() if res is None: break time.sleep(1) print('消费者消费了%s'% res) if __name__ == '__main__': # 启动队列->容器 q = Queue() # 生产者 p = Process(target=producer, args=(q, )) # 消费者 c = Process(target=consumer, args=(q, )) p.start() c.start() p.join() # 确保所有的生产者都生产完毕后才会给消费者发送结束信号(有几个消费者就要发几个结束信号) q.put(None) print('主进程')
from multiprocessing import Process,JoinableQueue import time # 生产者在消费者消费的同时不能生产,消费者在生产的同时不能消费 # 这样的问题会导致生产者和消费者之间彼此在互相等待 # 生产者消费者模型就是可以解决这样的问题, # 在生产者和消费者之间建立一个容器,生产者往容器里生产,消费者去容器里取,这样就解决了耦合问题 # 好处1:解耦合 # 好处2:平衡了数据处理的时间差 # 基于队列实现生产者消费者模型 # 回顾守护进程 def producer(q): for i in range(2): res = '制造品%s' % i time.sleep(0.5) print('生产者生产了%s' % res) q.put(res) q.join() def consumer(q): while True: res = q.get() if res is None: break time.sleep(1) print('消费者消费了%s'% res) q.task_done() if __name__ == '__main__': # 启动队列->容器,JoinableQueue可以执行Queue.join q = JoinableQueue() # 生产者 p = Process(target=producer, args=(q, )) # 消费者 c = Process(target=consumer, args=(q, )) c.daemon = True p.start() c.start() p.join() print('主进程')
# 方式一: # import time # import random # from threading import Thread # # # def piao(name): # print('%s piaoing' % name) # time.sleep(random.randrange(1,5)) # # if __name__ == '__main__': # t1 = Thread(target=piao, args=('panda',)) # t1.start() # # 站在执行的角度,这个就是主线程,站在资源的角度就是主进程 # print('主线程') # -------------------------------------------------------------------- # 方式二: # import time # import random # from threading import Thread # # class MyThread(Thread): # def __init__(self, name): # super().__init__() # self.name = name # # # def run(self): # print('%s is running' % self.name) # time.sleep(random.randrange(1,5)) # print('%s is ending' % self.name) # # if __name__ == '__main__': # t1 = MyThread('panda') # t1.start() # print('主线程')
# 1.开进程的开销远大于开线程的开销 # 进程在启动之后由于要操作系统给进程分配内存空间,所以执行的时间上没有线程那样快速 # import time # from threading import Thread # # # def piao(name): # print('%s piaoing' % name) # time.sleep(2) # # if __name__ == '__main__': # t1 = Thread(target=piao, args=('panda',)) # t1.start() # # 站在执行的角度,这个就是主线程,站在资源的角度就是主进程 # print('主线程') # -------------------------------------------------------------------- # 2.同一个进程内的多个线程共享该进程的地址内存空间 # from threading import Thread # from multiprocessing import Process # n = 100 # def task(): # global n # n = 0 # # # if __name__ == '__main__': # # p1 = Process(target=task, ) # # p1.start() # # p1.join() # t1 = Thread(target=task, ) # t1.start() # t1.join() # print('主线程', n) # -------------------------------------------------------------------- # 3.pid->进程的id号 # 每开一个进程就会给进程分配id号 # from threading import Thread # from multiprocessing import Process,current_process # import os # def task(): # print('线程%s' % os.getpid()) # # # if __name__ == '__main__': # # p1 = Process(target=task, ) # # p1.start() # # p1.join() # t1 = Thread(target=task, ) # t1.start() # t1.join() # print('主', os.getpid())
from threading import Thread,currentThread,active_count,enumerate import time def task(): print('%s is running' %currentThread().getName()) time.sleep(2) print('%s is done' % currentThread().getName()) if __name__ == '__main__': t = Thread(target=task, name='子线程-1') t.start() # 设置线程名称 # t.setName('子线程---1') # t.join() # 查看线程是否存活 # print(t.is_alive()) # print('主线程的名称',currentThread().getName()) # t.join() # 查看当前执行线程活跃数 # print(active_count()) # 显示列表格式的,把当前活跃的线程对象放入列表里 print(enumerate())
# from threading import Thread # import time # def sayhi(name): # time.sleep(2) # print('%s say hello' % name) # # if __name__ == '__main__': # t = Thread(target=sayhi, args=('panda',)) # t.setDaemon(True) # t.start() # print('主线程') # print(t.is_alive()) # ---------------------------------------------------------------------- from threading import Thread import time def foo(): print('start-123') time.sleep(1) print('end-123') def bar(): print('start-456') time.sleep(3) print('end-456') if __name__ == '__main__': t1 = Thread(target=foo) t2 = Thread(target=bar) t1.daemon = True t1.start() t2.start() print('main-----------')
#mutex from threading import Thread,Lock import time n = 100 def task(): global n mutex.acquire() temp = n time.sleep(0.1) n = temp-1 mutex.release() if __name__ == '__main__': mutex = Lock() t_l = [] for i in range(100): t = Thread(target=task) t_l.append(t) t.start() for t in t_l: t.join() print('主',n)
# from multiprocessing import Process # from threading import Thread # import os,time # def work(): # res = 0 # for i in range(10000000): # res *= i # if __name__ == '__main__': # l = [] # print(os.cpu_count()) # start = time.time() # for i in range(4): # # p = Process(target=work) # p = Thread(target=work) # l.append(p) # p.start() # for p in l: # p.join() # stop = time.time() # print('spend time %s'%(stop-start)) # ----------------------------------------------- from multiprocessing import Process from threading import Thread import threading import os,time def work(): time.sleep(2) print('===>') if __name__ == '__main__': l=[] print(os.cpu_count()) #本机为4核 start=time.time() for i in range(400): p=Process(target=work) #耗时12s多,大部分时间耗费在创建进程上 # p=Thread(target=work) #耗时2s多 l.append(p) p.start() for p in l: p.join() stop=time.time() print('run time is %s' %(stop-start))
# from threading import Thread,Lock # import time # mutexA = Lock() # mutexB = Lock() # # class MyThread(Thread): # def run(self): # self.f1() # self.f2() # def f1(self): # mutexA.acquire() # print('%s 拿到了A'% self.name) # mutexB.acquire() # print('%s 拿到了B' % self.name) # mutexB.release() # mutexA.release() # def f2(self): # mutexB.acquire() # print('%s 拿到了B' % self.name) # time.sleep(0.1) # mutexA.acquire() # print('%s 拿到了A'%self.name) # mutexA.release() # mutexB.release() # # # if __name__ == '__main__': # for i in range(10): # t = MyThread() # t.start() # ------------------------------------------------------------ # 递归锁:可以连续acquire多次,每一次acquire一次,计数器就+1,只要计数为0,才能被其他线程抢到 # from threading import Thread,RLock # import time # mutexB = mutexA = RLock() # # class MyThread(Thread): # def run(self): # self.f1() # self.f2() # def f1(self): # mutexA.acquire() # print('%s 拿到了A'% self.name) # mutexB.acquire() # print('%s 拿到了B' % self.name) # mutexB.release() # mutexA.release() # def f2(self): # mutexB.acquire() # print('%s 拿到了B' % self.name) # time.sleep(0.1) # mutexA.acquire() # print('%s 拿到了A'%self.name) # mutexA.release() # mutexB.release() # # # if __name__ == '__main__': # for i in range(10): # t = MyThread() # t.start()
from threading import Thread,Semaphore,currentThread import time import random sm = Semaphore(5) def task(): # print('%s in '%currentThread().getName()) with sm: print('%s in ' % currentThread().getName()) time.sleep(random.randint(1,3)) if __name__ == '__main__': for i in range(10): t = Thread(target=task) t.start()
from threading import Thread, Event import time event = Event() # event.wait() # event.set() def student(name): print('学生%s 正在听课' % name) event.wait(3) print('学生%s 课间活动' % name) def teacher(name): print('老师%s正在上课'%name) time.sleep(7) event.set() if __name__ == '__main__': stu1 = Thread(target=student, args=('panda',)) stu2 = Thread(target=student,args=('boy',)) stu3 = Thread(target=student, args=('zombie',)) t1 = Thread(target=teacher,args=('girl',)) stu1.start() stu2.start() stu3.start() t1.start()
from threading import Timer def task(name): print('hello %s' % name) t = Timer(5 , task , args=('panda',)) t.start() #----------------------------------------------------- import random from threading import Timer class Code: def __init__(self): self.make_cache() def make_cache(self, interval=3): self.cache = self.make_code() print(self.cache) self.t = Timer(interval, self.make_cache) self.t.start() def make_code(self,n=4): res = '' for i in range(n): s1 = str(random.randint(0,9)) s2 = chr(random.randint(65,90)) res += random.choice([s1,s2]) return res def check(self): while True: code = input('请输入验证码>>').strip() if code.upper() == self.cache: print('验证码输入正确') self.t.cancel() break obj = Code() obj.check()
import queue # 先进先出->队列功能 q = queue.Queue(3) q.put('first') q.put(2) q.put('third') q.put(4, block=True, timeout=3) # 放了多余的值就会因为锁的原因卡主 print(q.get()) print(q.get()) print(q.get())
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor from multiprocessing import Process import os import time import random # 线程池和进程池的接口是一样的,那么什么时候用进程池,什么时候用线程池 # 池本质就是开进程和线程,本质没有区别 # 计算密集型,需要用多核的优势时候就要用进程池 # I/O密集型,不需要过多CPU资源的时候就要用线程池 def task(name): print('name is %s pid is %s'%(name, os.getpid())) time.sleep(random.randint(1,3)) if __name__ == '__main__': # 进程池最多装4个进程 # pool = ThreadPoolExecutor(4) pool = ProcessPoolExecutor(4) for i in range(10): # 异步调用,提交完任务以后,不用等任务拿到结果,只负责任务做完 pool.submit(task,'panda %s'%i) pool.shutdown() print('主线程')
# 提交任务的两种方式 # 1.同步调用:提交完任务后,就等待任务执行完毕,拿到结果,再执行下一行代码 # 同步调用的结果就是串行执行的方式 # import time # import random # from concurrent.futures import ThreadPoolExecutor # # # def la(name): # print('%s is laing' % name) # time.sleep(random.randint(1,3)) # res = random.randint(7,13)*"*" # return {'name':name, 'res':res} # # # def weight(obj): # name = obj['name'] # size = obj['res'] # print('%s is %s'%(name, size)) # # # if __name__ == '__main__': # pool = ThreadPoolExecutor(10) # res1 = pool.submit(la, 'panda').result() # weight(res1) # res2 = pool.submit(la, 'zombie').result() # weight(res2) # res3 = pool.submit(la, 'boy').result() # weight(res3) # 2.异步调用:提交完任务以后不在原地等待任务执行完毕, import time import random from concurrent.futures import ThreadPoolExecutor def la(name): print('%s is laing' % name) time.sleep(random.randint(1,3)) res = random.randint(7,13)*"*" return {'name':name, 'res':res} def weight(obj): obj = obj.result() name = obj['name'] size = obj['res'] print('%s is %s'%(name, size)) if __name__ == '__main__': pool = ThreadPoolExecutor(10) pool.submit(la, 'panda').add_done_callback(weight) pool.submit(la, 'zombie').add_done_callback(weight) pool.submit(la, 'boy').add_done_callback(weight)
def producer(): g = consumer() next(g) for i in range(100000000): g.send(i) def consumer(): while True: res = yield producer()
from greenlet import greenlet def eat(name): print('%s eat 1' % name) g2.switch('panda') print('%s eat 2' % name) g2.switch() def play(name): print('%s play 1' % name) g1.switch() print('%s play 2' % name) g1 = greenlet(eat) g2 = greenlet(play) # 切换 g1.switch('panda')
import gevent import time from gevent import monkey # 打补丁,所有的设计IO操作的部分都全部打上补丁,做上标记 monkey.patch_all() def eat(name): print('%s eat 1' % name) time.sleep(3) print('%s eat 2' % name) def play(name): print('%s play 1' % name) time.sleep(5) print('%s play 2' % name) g1 = gevent.spawn(eat, 'panda') g2 = gevent.spawn(play, 'boy') g1.join() g2.join()
import gevent import time from gevent import monkey;monkey.patch_all() # 打补丁,所有的设计IO操作的部分都全部打上补丁,做上标记 def eat(name): print('%s eat 1' % name) time.sleep(3) print('%s eat 2' % name) def play(name): print('%s play 1' % name) time.sleep(5) print('%s play 2' % name) g1 = gevent.spawn(eat, 'panda') g2 = gevent.spawn(play, 'boy') gevent.joinall([g1,g2])
Win a contest, win a challenge
posted on 2018-07-31 21:52 pandaboy1123 阅读(208) 评论(0) 编辑 收藏 举报