摘要:
code # 文件db的内容为:{"count":5} # 注意一定要用双引号,不然json无法识别 # 并发运行,效率高,但竞争写同一文件,数据写入错乱 from multiprocessing import Process,Lock import time,json,random def sea 阅读全文
posted @ 2020-12-26 14:45
anobscureretreat
阅读(110)
评论(0)
推荐(0)
摘要:
code import time from multiprocessing import Queue, Process def f(q): q.put([time.asctime(), 'from Eva', 'hello']) # 调用主函数中p进程传递过来的进程参数,puy函数为向队列中添加一条 阅读全文
posted @ 2020-12-26 14:42
anobscureretreat
阅读(149)
评论(0)
推荐(0)
摘要:
code ''' multiprocessing模块支持进程间通信的两种主要形式:管道和队列 都是基于消息传递实现的,但是队列接口 ''' from multiprocessing import Queue q=Queue(3) #put ,get ,put_nowait,get_nowait,fu 阅读全文
posted @ 2020-12-26 14:39
anobscureretreat
阅读(114)
评论(0)
推荐(0)
摘要:
* 函数对象:可以将定义在函数内的函数返回到全局使用,从而打破函数的层级限制。 * 名称空间与作用域:作用域关系在函数定义阶段时就已经固定死了。与调用位置无关,即在任意位置调用函数都需要跑到定义函数时找到作用域关系 def f1(): x = 1 def inner(): print(x) retu 阅读全文
posted @ 2020-12-26 14:37
anobscureretreat
阅读(72)
评论(0)
推荐(0)
摘要:
code # 不加锁:并发执行,速度快,数据不安全 from threading import current_thread, Thread, Lock import os, time n = 100 def task(): global n print('%s is running' % curr 阅读全文
posted @ 2020-12-26 14:30
anobscureretreat
阅读(96)
评论(0)
推荐(0)
摘要:
有的同学可能有疑问:既然加锁会让运行变成串行,那么我在start之后立即使用join,就不用加锁了啊,也是串行的效果啊 没错:在start之后立刻使用jion,肯定会将100个任务的执行变成串行,毫无疑问,最终n的结果也肯定是0,是安全的,但问题是 start后立即join:任务内的所有代码都是串行 阅读全文
posted @ 2020-12-26 14:26
anobscureretreat
阅读(86)
评论(0)
推荐(0)
摘要:
code # 不加锁:未加锁部分并发执行,加锁部分串行执行,速度慢,数据安全 from threading import current_thread, Thread, Lock import os, time n = 100 def task(): # 未加锁的代码并发运行 time.sleep( 阅读全文
posted @ 2020-12-26 14:21
anobscureretreat
阅读(121)
评论(0)
推荐(0)
摘要:
code import threading from threading import Thread from threading import Condition import time import random c = Condition() # 条件锁 itemNum = 0 item = 阅读全文
posted @ 2020-12-26 14:06
anobscureretreat
阅读(123)
评论(0)
推荐(0)
摘要:
code # python3.2版本之后才有的; from concurrent.futures import ThreadPoolExecutor import time def job(num): print("这是一个%s任务" %(num)) return "执行结果:%s" %(num) 阅读全文
posted @ 2020-12-26 13:53
anobscureretreat
阅读(78)
评论(0)
推荐(0)
摘要:
例2:生产者-消费者模型,用继承实现 什么是生产者-消费者模型? 某个模块专门负责生产+数据, 可以认为是工厂; 另外一个模块负责对生产的数据进行处理的, 可以认为是消费者. 在生产者和消费者之间加个缓冲区(队列queue实现), 可以认为是商店. 生产者 》缓冲区 》 消费者 优点: 1). 解耦 阅读全文
posted @ 2020-12-26 13:50
anobscureretreat
阅读(88)
评论(0)
推荐(0)
摘要:
server #-*- coding:utf-8 -*- """ __author__ = BlingBling 建立TCP的基本流程 ss = socket() # 创建服务器套接字 ss.bind() # 套接字与地址绑定 ss.listen() # 监听连接 inf_loop: # 服务器无限 阅读全文
posted @ 2020-12-26 13:48
anobscureretreat
阅读(312)
评论(0)
推荐(0)
摘要:
code res.sort(key=lambda fn: os.path.getmtime(os.path.join(folder_storage,fn["name"])),reverse = True) # 方法一 import os def listdir(path, list_name): " 阅读全文
posted @ 2020-12-26 13:46
anobscureretreat
阅读(409)
评论(0)
推荐(0)
摘要:
code import threading from queue import Queue import time def timeit(f): def wrapper(*args, **kwargs): start_time = time.time() res = f(*args, **kwarg 阅读全文
posted @ 2020-12-26 13:40
anobscureretreat
阅读(195)
评论(0)
推荐(0)
摘要:
python使用多线程, 不一定运行速度快,这里引入GIL(global interpreter lock) python解释器中任意时刻都只有一个线程在执行; GIL执行过程: 1). 设置一个GIL; 2). 切换线程去准备执行任务(Runnale就绪状态); 3). 运行; 4). 可能出现的 阅读全文
posted @ 2020-12-26 13:36
anobscureretreat
阅读(321)
评论(0)
推荐(0)
摘要:
code import multiprocessing def foo(i): print ('called function in process: %s' %i) return if __name__ == '__main__': Process_jobs = [] for i in range 阅读全文
posted @ 2020-12-26 13:35
anobscureretreat
阅读(102)
评论(0)
推荐(0)
摘要:
1.1进程 之前我们已经了解了操作系统中进程的概念,程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程。程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述文本;进程是程序的一次执行活动,属于动态概念。在多道编程中,我们允许多个程序同时加载 阅读全文
posted @ 2020-12-26 13:31
anobscureretreat
阅读(68)
评论(0)
推荐(0)
摘要:
code from threading import Thread import os,time n=100 def work(): global n temp=n time.sleep(0.1) n=temp-1 l=[] for i in range(100): p=Thread(target= 阅读全文
posted @ 2020-12-26 13:17
anobscureretreat
阅读(120)
评论(0)
推荐(0)
摘要:
code import threading R=threading.Lock() R.acquire() ''' 对公共数据的操作 ''' R.release() 阅读全文
posted @ 2020-12-26 13:16
anobscureretreat
阅读(74)
评论(0)
推荐(0)
摘要:
所谓死锁:是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去。此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程,如下就是死锁 code from threading import Thread,Lock i 阅读全文
posted @ 2020-12-26 13:07
anobscureretreat
阅读(161)
评论(0)
推荐(0)
摘要:
同进程的一样 Semaphore管理一个内置的计数器, 每当调用acquire()时内置计数器-1; 调用release() 时内置计数器+1; 计数器不能小于0;当计数器为0时,acquire()将阻塞线程直到其他线程调用release()。 实例:(同时只有5个线程可以获得semaphore,即 阅读全文
posted @ 2020-12-26 13:00
anobscureretreat
阅读(149)
评论(0)
推荐(0)
摘要:
解决:典型死锁问题科学家吃面 code import time from threading import Thread, RLock fork_lock = noodle_lock = RLock() def eat1(name): noodle_lock.acquire() print('%s 阅读全文
posted @ 2020-12-26 12:52
anobscureretreat
阅读(88)
评论(0)
推荐(0)
摘要:
code import time from threading import Thread,Lock noodle_lock = Lock() fork_lock = Lock() def eat1(name): noodle_lock.acquire() print('%s 抢到了面条'%name 阅读全文
posted @ 2020-12-26 12:49
anobscureretreat
阅读(222)
评论(0)
推荐(0)
摘要:
安装 pip3 install gevent Gevent 是一个第三方库,可以轻松通过gevent实现并发同步或异步编程,在gevent中用到的主要模式是Greenlet, 它是以C扩展模块形式接入Python的轻量级协程。 Greenlet全部运行在主程序操作系统进程的内部,但它们被协作式地调度 阅读全文
posted @ 2020-12-26 12:45
anobscureretreat
阅读(540)
评论(0)
推荐(1)
摘要:
code import copy age = 19 print(f'first:{id(age)}') age = 20 print(f'second:{id(age)}') l1 = ['a', 'b', 'c', ['d', 'e', 'f']] l2 = l1 print("*"*25,"赋值 阅读全文
posted @ 2020-12-26 12:40
anobscureretreat
阅读(81)
评论(0)
推荐(0)
摘要:
安装 pip3 install pyinstaller 更改exe文件的图标 pyinstaller -F --icon=1234.ico run.py 打包成exe文件夹 pyinstaller run.py 打包成一个exe文件 pyinstaller -F run.py 阅读全文
posted @ 2020-12-26 12:38
anobscureretreat
阅读(84)
评论(0)
推荐(0)
摘要:
上一篇记录了关于文件夹下文件的遍历的程序实现,今天学习一下关于文件的复制、重命名、移动和删除的操作; 对于文件的操作,主要依赖OS模块和shutil模块; 下面直接以例子来说明关于文件的复制、重命名、移动和删除的操作:一、复制 1、文件的复制 import os import shutil shut 阅读全文
posted @ 2020-12-26 12:35
anobscureretreat
阅读(361)
评论(0)
推荐(0)
摘要:
urllib.request 返回的数据需要解码,如 网站返回的是GBK编码数据. 需要调用decode("gbk") 此时输出不会乱码. with urllib.request.urlopen(url, context=context) as response: html = response.r 阅读全文
posted @ 2020-12-26 12:28
anobscureretreat
阅读(472)
评论(0)
推荐(0)
摘要:
code from threading import Thread from multiprocessing import Process import os def work(name): print('{} say hello'.format(name)) if __name__ == '__m 阅读全文
posted @ 2020-12-26 12:04
anobscureretreat
阅读(75)
评论(0)
推荐(0)

浙公网安备 33010602011771号