09 2018 档案

摘要: 阅读全文
posted @ 2018-09-14 11:14 Woowo 阅读(55) 评论(0) 推荐(0)
摘要:from greenlet import greenlet import time def test1(): while True: print('----A----') g2.switch() time.sleep(0.5) def test2(): while True: print('----B----')... 阅读全文
posted @ 2018-09-13 18:13 Woowo 阅读(120) 评论(0) 推荐(0)
摘要:def fib(num): a, b = 0, 1 index = 0 while index < num: yield a a, b = b, a+b index += 1 return 'ok...' f = fib(100) while True: try: print(next(f... 阅读全文
posted @ 2018-09-13 16:50 Woowo 阅读(148) 评论(0) 推荐(0)
摘要:class ClassMate(object): def __init__(self): self.names = list() self.index = 0 def add(self,name): self.names.append(name) def __iter__(self): return se... 阅读全文
posted @ 2018-09-13 15:29 Woowo 阅读(122) 评论(0) 推荐(0)
摘要:多线程udp聊天器 阅读全文
posted @ 2018-09-12 22:27 Woowo 阅读(132) 评论(0) 推荐(0)
摘要:非阻塞 阅读全文
posted @ 2018-09-12 10:38 Woowo 阅读(121) 评论(0) 推荐(0)
摘要:def consumer(): while True: x = yield print('处理了数据:',x) def producer(): c = consumer() next(c) for i in range(10): print('生产了数据:',i) c.send(i) produce... 阅读全文
posted @ 2018-09-11 16:01 Woowo 阅读(100) 评论(0) 推荐(0)
摘要:import time from concurrent.futures import ThreadPoolExecutor def func(n): time.sleep(2) print(n) return n*n tpool = ThreadPoolExecutor(max_workers=5) t_lst = [] for i in range(20): ... 阅读全文
posted @ 2018-09-11 09:37 Woowo 阅读(103) 评论(0) 推荐(0)
摘要:from threading import Thread,Semaphore import time def func(sem,a,b): sem.acquire() time.sleep(0.5) print(a+b) sem.release() sem = Semaphore(4) t_lst = [] for i in range(10): t ... 阅读全文
posted @ 2018-09-10 18:57 Woowo 阅读(140) 评论(0) 推荐(0)
摘要:from threading import Lock,Thread import time def func(): global n temp = n time.sleep(0.2) n = temp -1 n = 10 t_lst = [] for i in range(10): t = Thread(target=func) t.start(... 阅读全文
posted @ 2018-09-10 11:03 Woowo 阅读(132) 评论(0) 推荐(0)
摘要:import time from threading import Thread def func1(): while True: print('*'*10) time.sleep(1) def func2(): print('in func2') time.sleep(5) t = Thread(target=func1,) t.d... 阅读全文
posted @ 2018-09-10 09:20 Woowo 阅读(92) 评论(0) 推荐(0)
摘要:import time from threading import Thread def func(n): time.sleep(1) print(n) for i in range(10): t = Thread(target=func,args=(i,)) t.start() from threading import Thread import tim... 阅读全文
posted @ 2018-09-09 20:01 Woowo 阅读(99) 评论(0) 推荐(0)
摘要:from multiprocessing import Pool,Process import time def func(n): for i in range(10): print(n+1) if __name__ == '__main__': st1 = time.time() pool = Pool(5) pool.map(func,ra... 阅读全文
posted @ 2018-09-09 10:10 Woowo 阅读(126) 评论(0) 推荐(0)
摘要:from multiprocessing import Pipe,Process def func(conn1): while True: msg = conn1.recv() if msg is None: break print(msg) if __name__ == '__main__': conn1,conn2 = Pi... 阅读全文
posted @ 2018-09-08 22:26 Woowo 阅读(154) 评论(0) 推荐(0)
摘要:from multiprocessing import Manager,Process def main(dic): dic['count'] -= 1 print(dic) if __name__ == '__main__': m = Manager() dic = m.dict({'count':100}) p_dict = [] p = ... 阅读全文
posted @ 2018-09-08 20:56 Woowo 阅读(339) 评论(0) 推荐(0)
摘要:JoinableQueue 阅读全文
posted @ 2018-09-08 17:11 Woowo 阅读(118) 评论(0) 推荐(0)
摘要:from multiprocessing import Queue,Process def producer(q): q.put('hello') def consumer(q): print(q.get()) if __name__ == '__main__': q = Queue() p = Process(target=producer,args=(q... 阅读全文
posted @ 2018-09-08 10:34 Woowo 阅读(94) 评论(0) 推荐(0)
摘要:import time import random from multiprocessing import Process,Event def cars(e,i): if not e.is_set(): print('car%d在等待'%i) e.wait() print('\033[33mcar%i通过\033[0m' % i) def l... 阅读全文
posted @ 2018-09-07 20:51 Woowo 阅读(153) 评论(0) 推荐(0)
摘要:from multiprocessing import Lock from multiprocessing import Process import json import time # def show_ticket(i): # with open('ticket') as f: # dic = json.load(f) # print('%d:余票%s'%... 阅读全文
posted @ 2018-09-07 10:05 Woowo 阅读(142) 评论(0) 推荐(0)
摘要:from multiprocessing import Process import time def func(): while True: time.sleep(0.2) print('我还活着') def func2(): print('in func2 start') time.sleep(8) print('in fu... 阅读全文
posted @ 2018-09-06 20:56 Woowo 阅读(104) 评论(0) 推荐(0)
摘要:import socket from multiprocessing import Process def server(conn): msg = '连接成功'.encode('utf-8') conn.send(msg) msg2 = conn.recv(1024).decode('utf-8') print(msg2) conn.close() i... 阅读全文
posted @ 2018-09-06 20:13 Woowo 阅读(121) 评论(0) 推荐(0)
摘要:import time from multiprocessing import Process def func(arg1,arg2): print('*'*arg1) time.sleep(5) print('*'*arg2) if __name__ == '__main__': p = Process(target=func,args=(10,20)) ... 阅读全文
posted @ 2018-09-06 19:05 Woowo 阅读(316) 评论(0) 推荐(0)
摘要:开启多个子进程 多进程之间的数据隔离问题 开启多进程的第二种方式 阅读全文
posted @ 2018-09-06 09:10 Woowo 阅读(113) 评论(0) 推荐(0)
摘要:server client 阅读全文
posted @ 2018-09-05 17:09 Woowo 阅读(220) 评论(0) 推荐(0)
摘要:tcp客户端 tcp服务器 阅读全文
posted @ 2018-09-04 10:10 Woowo 阅读(132) 评论(0) 推荐(0)
摘要:服务器端sockforever版 client端 阅读全文
posted @ 2018-09-02 22:46 Woowo 阅读(111) 评论(0) 推荐(0)
摘要:server端 client端 阅读全文
posted @ 2018-09-02 20:35 Woowo 阅读(127) 评论(0) 推荐(0)
摘要:client端执行命令 server端发送命令 阅读全文
posted @ 2018-09-02 17:20 Woowo 阅读(120) 评论(0) 推荐(0)
摘要:import subprocess res = subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) print('stdout:',res.stdout.read().decode('gbk')) print('stderr:',res.stderr.read().decode('gbk... 阅读全文
posted @ 2018-09-02 16:27 Woowo 阅读(90) 评论(0) 推荐(0)
摘要:server端 client端 阅读全文
posted @ 2018-09-02 16:07 Woowo 阅读(191) 评论(0) 推荐(0)
摘要:server端 client端 阅读全文
posted @ 2018-09-02 15:54 Woowo 阅读(176) 评论(0) 推荐(0)
摘要:#server端 import socket ip_port = ('127.0.0.1',8080) sk = socket.socket() sk.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) sk.bind(ip_port) sk.listen() conn,addr = sk.accept() while True: ... 阅读全文
posted @ 2018-09-02 15:08 Woowo 阅读(773) 评论(0) 推荐(0)
摘要:import sys def processBar(num, total): rate = num / total rate_num = int(rate * 100) if rate_num == 100: r = '\r%s>%d%%\n' % ('=' * rate_num, rate_num,) else: r = '\r... 阅读全文
posted @ 2018-09-01 20:40 Woowo 阅读(116) 评论(0) 推荐(0)
摘要:验证客户端链接的合法性 如果你想在分布式系统中实现一个简单的客户端链接认证功能,又不像SSL那么复杂,那么利用hmac+加盐的方式来实现 客户端(合法) 阅读全文
posted @ 2018-09-01 19:51 Woowo 阅读(126) 评论(0) 推荐(0)
摘要:#加入一条socket配置,重用ip和端口 import socket from socket import SOL_SOCKET,SO_REUSEADDR sk = socket.socket() sk.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) #就是它,在bind前加 sk.bind(('127.0.0.1',8898)) #把地址绑定到套接字 sk.li... 阅读全文
posted @ 2018-09-01 16:29 Woowo 阅读(107) 评论(0) 推荐(0)