from threading import Thread import random def fun(arg): flag = True for i in range(2,arg): if arg % i == 0: flag = False break if flag: print('质数为:%d'%arg) if __name__ == '__main__': for num in range(100,301): l = ['a','b','c','d'] r = random.choice(l) if r == 'a': thr1 = Thread(target = fun,args=(num,)) thr1.start() thr1.join() elif r=='b': thr2 = Thread(target = fun,args=(num,)) thr2.start() thr2.join() elif r=='c': thr3 = Thread(target = fun,args =(num, )) thr3.start() thr3.join() else: thr4 = Thread(target =fun,args = (num,) ) thr4.start() thr4.join()
1.什么是面向对象的 mro
获取类的继承顺序,
2.joson序列化时,默认遇到中文会转换为unicode,如果想要保留中文该怎么办
在dumpling函数中添加参数ensure_ascii=False
9.如何在function里面设置一个全局变量
在你将要设置全局前加上global
11.python里的多线程机制和多进程的参数传递方式
python下的多线程有个全局解释器锁。它的意思是任一时间只有一个线程运用解释器,和单个CPU跑多个程序是一个意思这是并发而不是并行,
多进程间共享数据
12.创建一个文件,文件的第一行写入0,在创建20个任务线程,他们同时打开文件并将文件第一行读出来,加1写回去,将原有的文件值覆盖掉,请保证20个任务线程完成后,文件呢的第一行一定是20:
from threading import Thread,Lock lock = Lock() def read(file): with open(file,mode='r+',encoding ='utf-8' ) as f: lock.acquire() res=int(f.read()) print(res) f.seek(0,0) f.write(str(res+1)) lock.release() if __name__ == '__main__': l=[] for i in range(20): l.append(Thread(target=read,args=('./file',))) for i in range(20): l[i].start() for i in range(20): l[i].join()