09 2018 档案
摘要:from greenlet import greenlet import time def test1(): while True: print('----A----') g2.switch() time.sleep(0.5) def test2(): while True: print('----B----')...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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): ...
阅读全文
摘要: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 ...
阅读全文
摘要: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(...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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 = ...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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'%...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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)) ...
阅读全文
摘要:服务器端sockforever版 client端
阅读全文
摘要: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...
阅读全文
摘要:#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: ...
阅读全文
摘要: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...
阅读全文
摘要:验证客户端链接的合法性 如果你想在分布式系统中实现一个简单的客户端链接认证功能,又不像SSL那么复杂,那么利用hmac+加盐的方式来实现 客户端(合法)
阅读全文
摘要:#加入一条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...
阅读全文

浙公网安备 33010602011771号