随笔分类 - Python
摘要:默认Path converter Django2.0自带的PathConveter包括: str:匹配除了路径分隔符(/)之外的非空字符串,如果没有转换器,默认使用str作为转换器。 int:匹配0及正整数。 slug:匹配字母、数字以及横杠、下划线组成的字符串。 uuid:匹配格式化的uuid,如
阅读全文
摘要:1.安装virtualenv pip install virtualenv 2.创建虚拟环境 virtualenv [虚拟环境名字] 3.进入虚拟环境 3.7版本~进入到创建的虚拟环境文件夹下面的Scripts文件夹下,即进入虚拟环境。之前版本~(在Scripts文件下执行activate命令进入虚
阅读全文
摘要:字符组:[] 写再括号中的内容,若出现,都可以被匹配到。 [0-9] 匹配数字 [a-z] 匹配小写字母 [A-Z] 匹配大写字母 [a-zA-Z] 匹配所有的大写和小写的字母 [a-zA-Z0-9] 匹配所有的大写和小写的字母和数字 [a-zA-Z0-9_] 匹配所有的大写和小写的字母和数字和下划
阅读全文
摘要:1.协程 1.本质上是一个线程。 2.能偶在多个任务之间切换来节省一下IO时间。 3.协程中任务之间的切换也消耗时间,但开销要远远小于进程和线程之间的切换。 真正的协程模块就是使用greenlet来完成多个任务之间的切换。 from greenlet import greenlet def eat(
阅读全文
摘要:1.线程池 import time from concurrent.futures import ThreadPoolExecutor def func(n): time.sleep(2) print(n) t_pool = ThreadPoolExecutor(max_workers=5) # 创
阅读全文
摘要:1.互斥锁 import time from threading import Thread,Lock def func1(lock): global n lock.acquire() #加锁 temp = n time.sleep(0.2) n = temp -1 lock.release() #
阅读全文
摘要:1.多线程并发 from threading import Thread import time def func(n): time.sleep(1) print(n) for i in range(10): t = Thread(target=func,args=(i,)) #将函数注册进子线程,
阅读全文
摘要:1.进程池 from multiprocessing import Pool def func(n): for i in range(10): print(n+1) if __name__ == '__main__': pool = Pool(3) #启动有三个进程的进程池。 #第一个参数进程要访问
阅读全文
摘要:1.数据共享 实现进程之间的数据共享 from multiprocessing import Manager,Process class MyPro(Process): def __init__(self,dic): super().__init__() self.dic = dic def run
阅读全文
摘要:1.管道 from multiprocessing import Pipe conn1,conn2 = Pipe() #返回两个值 conn1.send('wdc') #发送 print(conn2.recv()) #接收 conn2.send('yhf') print(conn1.recv())
阅读全文
摘要:消费者和生产者模型 from multiprocessing import Process,Queue import time import random class Producer(Process): def __init__(self,name,food,q): super().__init_
阅读全文
摘要:1.队列 from multiprocessing import Queue q = Queue(5) #创建队列对象,队列大小为5,队列中只能存放5个元素 q.put(1) #往队列中添加元素 q.put(2) q.put(3) q.put(4) q.put(5) print(q.full())
阅读全文
摘要:1.事件 :通过一个信号来控制多个进程同时执行或者阻塞。 一个信号可以使所有的进程都进入阻塞状态,也可以控制所有的进程接触阻塞,一个事件被创建之后,默认是阻塞状态。 from multiprocessing import Event e = Event() #创建事件对象 print(e.is_se
阅读全文
摘要:多进程模拟买票~ import time import json from multiprocessing import Process class Show(Process): #查 def run(self): with open('ticket') as f: dic = json.load(
阅读全文
摘要:1.守护进程 守护进程会随着主进程的代码执行结束而结束。 语法:进程对象.daemon = True时,表示将进程设置为守护进程,一定在start之前设置。 import time from multiprocessing import Process class MyProcess(Process
阅读全文
摘要:1.多进程的第二种启动方式 import os from multiprocessing import Process # 创建一个自定义类,继承Process类 class MyProcess(Process): # 必须实现一个run方法,run方法中是子进程中执行的代码 def run(sel
阅读全文
摘要:from multiprocessing import Process import os def func1(): print('子进程1',os.getpid()) #子进程:获取当前进程的进程号 print('子进程的父进程:', os.getppid()) #获取进程的父进程id def f
阅读全文
摘要:# 给动态生产的类定义一个方法 def __init__(self,name): self.name = name print(self.name) def take(self,obj): print(obj) # 动态生成一个类type('类名',(父类1,父类2,),{字典:属性或方法}) Do
阅读全文
摘要:1.__str__和__repe__ class Person(object): def __init__(self,name,age): self.name = name self.age = age def __str__(self): return 'stf:我叫{},今年{}岁'.forma
阅读全文
摘要:class User(object): def denglu(self): print('欢迎来到登录页面!') def zhuce(self): print('欢迎来到注册页面!') def youke(self): print('欢迎来到游客页面!') u = User() while True
阅读全文

浙公网安备 33010602011771号