摘要: 服务端 from socket import * import time server = socket(AF_INET,SOCK_STREAM) server.bind(('127.0.0.1',8081)) server.listen(3) server.setblocking(False) #accept变为非阻塞状态, print('server startting...') ... 阅读全文
posted @ 2017-09-01 18:49 前方、有光 阅读(351) 评论(0) 推荐(0)
摘要: 服务端 from socket import * server = socket(AF_INET,SOCK_STREAM) server.bind(('127.0.0.1',8080)) server.listen(3) print('server startting...') while True: conn,addr = server.accept() print(addr... 阅读全文
posted @ 2017-09-01 18:43 前方、有光 阅读(367) 评论(0) 推荐(0)
摘要: 异步执行比同步执行根据任务量可以快数倍乃至n倍 阅读全文
posted @ 2017-08-31 16:52 前方、有光 阅读(528) 评论(0) 推荐(0)
摘要: 遇到IO阻塞自动切换(推荐) 阅读全文
posted @ 2017-08-31 16:14 前方、有光 阅读(388) 评论(0) 推荐(0)
摘要: 1 from concurrent.futures import ProcessPoolExecutor 2 import os,time,random 3 def task(n): 4 print('%s is running'%os.getpid()) 5 time.sleep(random.r 阅读全文
posted @ 2017-08-31 15:47 前方、有光 阅读(365) 评论(0) 推荐(0)
摘要: 1 import queue 2 q = queue.Queue() #模拟队列,先进先出 3 q.put('first') 4 q.put('second') 5 q.put('third') 6 7 print(q.get()) 8 print(q.get()) 9 print(q.get()) 阅读全文
posted @ 2017-08-30 15:15 前方、有光 阅读(357) 评论(0) 推荐(0)
摘要: 这个模块是我遇到的最简单的了。。。。。。。。。。。 阅读全文
posted @ 2017-08-30 15:00 前方、有光 阅读(329) 评论(0) 推荐(0)
摘要: from threading import Thread,Event,currentThread import time e = Event() def conn_mysql(): count = 1 while e.is_set(): #没收到信号执行 if count>3: #超过三次,弹出错误 raise Connectio... 阅读全文
posted @ 2017-08-30 14:58 前方、有光 阅读(337) 评论(0) 推荐(0)
摘要: 死锁↑ 递归锁↓ 阅读全文
posted @ 2017-08-30 14:56 前方、有光 阅读(407) 评论(0) 推荐(0)
摘要: 1 from multiprocessing import Process 2 from threading import Thread 3 import threading 4 import os,time 5 def work(): 6 time.sleep(2) 7 print(' >') 8 阅读全文
posted @ 2017-08-30 14:53 前方、有光 阅读(452) 评论(0) 推荐(0)
摘要: 在一个进程字儿开启多个线程与开启多个进程的区别 线程与进程的区别 线程中所有的数据共享 守护线程与守护进程的异同: 主线程会等到所有非守护线程结束,才销毁守护线程 主进程的代码运行完毕,就会销毁守护进程,然后等非守护进程运行完毕,主进程结束。 1 from threading import Thre 阅读全文
posted @ 2017-08-29 15:04 前方、有光 阅读(546) 评论(0) 推荐(0)
摘要: from multiprocessing import Process,Lock import os,time def walk(mutex): mutex.acquire() #为了防止偷看,记得上锁 print('task[%s] 洗澡'%os.getpid()) time.sleep(2) print('task[%s] 洗完澡'%os.... 阅读全文
posted @ 2017-08-25 17:00 前方、有光 阅读(497) 评论(0) 推荐(1)
摘要: 这个代码被我修改乱了,抱歉 阅读全文
posted @ 2017-08-25 16:59 前方、有光 阅读(507) 评论(0) 推荐(0)
摘要: from multiprocessing import Process,Lock import json,time,random,os def piao(mutex): mutex.acquire() #作用:按顺序购票,不会插队 dic = json.load(open('db.txt')) #读取,查看车票总数 if dic['count'] > 0: ... 阅读全文
posted @ 2017-08-25 16:54 前方、有光 阅读(668) 评论(0) 推荐(0)
摘要: Python并发编程_多进程 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程。Python提供了非常好用的多进程包multiprocessing。 mul 阅读全文
posted @ 2017-08-25 14:33 前方、有光 阅读(596) 评论(0) 推荐(0)