上一页 1 2 3 4 5 6 ··· 68 下一页
摘要: code1 from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor from threading import currentThread from multiprocessing import current_pr 阅读全文
posted @ 2020-12-26 18:57 anobscureretreat 阅读(136) 评论(0) 推荐(0) 编辑
摘要: 展望未来,基于消息传递的并发编程是大势所趋 即便是使用线程,推荐做法也是将程序设计为大量独立的线程集合,通过消息队列交换数据。 这样极大地减少了对使用锁定和其他同步手段的需求,还可以扩展到分布式系统中。 但进程间应该尽量避免通信,即便需要通信,也应该选择进程安全的工具来避免加锁带来的问题。 以后我们 阅读全文
posted @ 2020-12-26 18:48 anobscureretreat 阅读(132) 评论(0) 推荐(0) 编辑
摘要: code from multiprocessing import Process,Queue import time,random,os def consumer(q): while True: res=q.get() if res is None:break #收到结束信号则结束 time.sle 阅读全文
posted @ 2020-12-26 18:37 anobscureretreat 阅读(84) 评论(0) 推荐(0) 编辑
摘要: 当需要创建的子进程数量不多时,可以直接利用multiprocessing中的Process动态成生多个进程,但如果是上百甚至上千个目标,手动的去创建进程的工作量巨大,此时就可以用到multiprocessing模块提供的Pool方法。 初始化Pool时,可以指定一个最大进程数,当有新的请求提交到Po 阅读全文
posted @ 2020-12-26 18:11 anobscureretreat 阅读(459) 评论(0) 推荐(0) 编辑
摘要: code from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor from threading import currentThread from multiprocessing import current_pro 阅读全文
posted @ 2020-12-26 18:07 anobscureretreat 阅读(93) 评论(0) 推荐(0) 编辑
摘要: 先进先出 code import queue q=queue.Queue() q.put('first') q.put('second') q.put('third') print(q.get()) print(q.get()) print(q.get()) outputs macname@Macd 阅读全文
posted @ 2020-12-26 18:05 anobscureretreat 阅读(141) 评论(0) 推荐(0) 编辑
摘要: 无论是进程还是线程,都遵循:守护XX会等待主XX运行完毕后销毁。 需要强调的是:运行完毕并非终止运行。 对主进程来说,运行完毕指的是主进程代码运行完毕 对主线程来说,运行完毕指的是主线程所在的进程内所有非守护线程统统运行完毕,主线程才算运行完毕。 1.1详细解释 主进程在其代码结束后就已经算运行完毕 阅读全文
posted @ 2020-12-26 17:53 anobscureretreat 阅读(107) 评论(0) 推荐(0) 编辑
摘要: 同步 import os,time from multiprocessing import Pool def work(n): print('%s run' %os.getpid()) time.sleep(3) return n**2 if __name__ == '__main__': p=Po 阅读全文
posted @ 2020-12-26 17:47 anobscureretreat 阅读(81) 评论(0) 推荐(0) 编辑
摘要: server #Pool内的进程数默认是cpu核数,假设为4(查看方法os.cpu_count()) #开启6个客户端,会发现2个客户端处于等待状态 #在每个进程内查看pid,会发现pid使用为4个,即多个客户端公用4个进程 from socket import * from multiproces 阅读全文
posted @ 2020-12-26 17:44 anobscureretreat 阅读(80) 评论(0) 推荐(0) 编辑
摘要: code from multiprocessing import Pool import requests import json import os def get_page(url): print('<进程%s> get %s' %(os.getpid(),url)) respone=reque 阅读全文
posted @ 2020-12-26 17:41 anobscureretreat 阅读(109) 评论(0) 推荐(0) 编辑
摘要: code from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor from threading import currentThread from multiprocessing import current_pro 阅读全文
posted @ 2020-12-26 17:38 anobscureretreat 阅读(300) 评论(0) 推荐(0) 编辑
摘要: code from threading import Thread import time def sayhi(name): time.sleep(2) print('%s say hello' % name) if __name__ == '__main__': t = Thread(target 阅读全文
posted @ 2020-12-26 17:37 anobscureretreat 阅读(54) 评论(0) 推荐(0) 编辑
摘要: server import multiprocessing import threading import socket s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.bind(('127.0.0.1',8080)) s.listen(5) 阅读全文
posted @ 2020-12-26 17:28 anobscureretreat 阅读(139) 评论(0) 推荐(0) 编辑
摘要: code import re from urllib.request import urlopen from multiprocessing import Pool def get_page(url,pattern): response=urlopen(url).read().decode('utf 阅读全文
posted @ 2020-12-26 17:24 anobscureretreat 阅读(74) 评论(0) 推荐(0) 编辑
摘要: code from threading import Thread import threading from multiprocessing import Process import os def work(): import time time.sleep(3) print("thread s 阅读全文
posted @ 2020-12-26 17:22 anobscureretreat 阅读(58) 评论(0) 推荐(0) 编辑
摘要: code from multiprocessing import Pool import time,random,os def work(n): time.sleep(1) return n**2 if __name__ == '__main__': p=Pool() res_l=[] for i 阅读全文
posted @ 2020-12-26 17:20 anobscureretreat 阅读(249) 评论(0) 推荐(0) 编辑
摘要: code from threading import Thread from multiprocessing import Process import os def work(): global n n = 0 if __name__ == '__main__': n=100 p=Process( 阅读全文
posted @ 2020-12-26 17:15 anobscureretreat 阅读(258) 评论(0) 推荐(0) 编辑
摘要: 线程与进程的区别可以归纳为以下4点: 1.地址空间和其他资源(如打开文件):进程间相互独立,统一进程的个线程间共享。某进程内的线程在其他进程不可见。 2.通信:进程间通信IPC,线程间可以直接读写进程数据段(如全局变量)来进行通信—需要进程同步和互斥手段的辅助,以保证数据的一致性。 3.调度和切换: 阅读全文
posted @ 2020-12-26 17:08 anobscureretreat 阅读(111) 评论(0) 推荐(0) 编辑
摘要: 开启一个字处理软件进程,该进程肯定需要办不止一件事情,比如监听键盘输入,处理文字,定时自动将文字保存到硬盘,这三个任务操作的都是同一块数据,因而不能用多进程。只能在一个进程里并发地开启三个线程,如果是单线程,那就只能是,键盘输入时,不能处理文字和自动保存,自动保存时又不能输入和处理文字 阅读全文
posted @ 2020-12-26 17:07 anobscureretreat 阅读(73) 评论(0) 推荐(0) 编辑
摘要: code from threading import Thread from multiprocessing import Process import os def work(name): print('{}的pid是'.format(name), os.getpid()) if __name__ 阅读全文
posted @ 2020-12-26 17:04 anobscureretreat 阅读(208) 评论(0) 推荐(0) 编辑
摘要: 多个线程共享同一个进程的地址空间中的资源,是对一台计算机上多个进程的模拟,有时也称线程为轻量级的进程。 而对一台计算机上多个进程,则共享物理内存、磁盘、打印机等其他物理资源。多线程的运行与多进程的运行类似,是CPU在多个线程之间的快速切换。 不同的进程之间是充满敌意的,彼此是抢占、竞争CPU的关系, 阅读全文
posted @ 2020-12-26 17:02 anobscureretreat 阅读(130) 评论(0) 推荐(0) 编辑
摘要: Python提供两个模块进行多线程的操作,分别是thread和threading, 前者是比较低级的模块,用于更底层的操作,一般应用级别的开发不常用。 方法1:直接使用threading.Thread() import threading # 这个函数名可随便定义 def run(n): print 阅读全文
posted @ 2020-12-26 16:55 anobscureretreat 阅读(113) 评论(0) 推荐(0) 编辑
摘要: code import time import random from multiprocessing import Pool, Manager # 生产者 def producer(q, i): food = 'Spam-%d' % i time.sleep(random.uniform(1, 2 阅读全文
posted @ 2020-12-26 16:49 anobscureretreat 阅读(113) 评论(0) 推荐(0) 编辑
摘要: code import time import random from multiprocessing import Process, Queue # 生产者 def producer(q, i): food = 'Spam-%d' % i time.sleep(random.uniform(2, 阅读全文
posted @ 2020-12-26 16:45 anobscureretreat 阅读(211) 评论(0) 推荐(0) 编辑
摘要: code import time import random from multiprocessing import Queue # 生产者 def producer(q, num): for i in range(1, num + 1): food = 'Spam-%d' % i # time.s 阅读全文
posted @ 2020-12-26 16:40 anobscureretreat 阅读(70) 评论(0) 推荐(0) 编辑
摘要: GIL本质就是一把互斥锁,既然是互斥锁,所有互斥锁的本质都一样,都是将并发运行变成串行,以此来控制同一时间内共享数据只能被一个任务所修改,进而保证数据安全。 # 为何要有GIL? 因为Cpython解释器自带垃圾回收机制不是线程安全的。 # 如果不对垃圾回收机制线程做任何处理,也没有GIL锁行不行? 阅读全文
posted @ 2020-12-26 16:37 anobscureretreat 阅读(101) 评论(0) 推荐(0) 编辑
摘要: #主进程等 >p1,p2,p3等 >c1,c2 #p1,p2,p3结束了, 证明c1,c2肯定全都收完了p1,p2,p3发到队列的数据 #因而c1,c2也没有存在的价值了,不需要继续阻塞在进程中影响主进程了。应该随着主进程的结束而结束,所以设置成守护进程就可以了 code from multipro 阅读全文
posted @ 2020-12-26 16:34 anobscureretreat 阅读(84) 评论(0) 推荐(0) 编辑
摘要: Sikuli脚本自动化,你在屏幕上看到的任何东西。它使用图像识别,识别和控制GUI组件。这是有用的,当有一个GUI的内部或源代码的访问是不容易的。 Sikuli(在墨西哥维乔印第安人的语言里是”上帝之眼”的意思)是由美国麻省理工学院开发的一种最新编程技术,使得编程人员可以使用截图替代代码,从而简化代 阅读全文
posted @ 2020-12-26 16:32 anobscureretreat 阅读(327) 评论(0) 推荐(0) 编辑
摘要: 之前我们学习了线程、进程的概念,了解了在操作系统中进程是资源分配的最小单位,线程是CPU调度的最小单位。按道理来说我们已经算是把CPU的利用率提高很多了。但是我们知道无论是创建多进程还是创建多线程来解决问题,都要消耗一定的时间来创建进程、创建线程、以及管理他们之间的切换。 随着我们对于效率的追求不断 阅读全文
posted @ 2020-12-26 16:28 anobscureretreat 阅读(83) 评论(0) 推荐(0) 编辑
摘要: code import time def time_count(func): def wrapper(*args, **kwargs): start = time.time() res = func(*args, **kwargs) end = time.time() print(f'{func} 阅读全文
posted @ 2020-12-26 16:25 anobscureretreat 阅读(49) 评论(0) 推荐(0) 编辑
摘要: 进程就是正在执行的一个过程,进程是对正在运行程序的一个抽象。 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一。操作系统的其他所有内容都是围绕进程的概念展开的。 所以想要真正了解进程,必须事先了解操作系统。 PS:即使可以利用的cpu只有一个(早期的 阅读全文
posted @ 2020-12-26 16:21 anobscureretreat 阅读(65) 评论(0) 推荐(0) 编辑
摘要: code from multiprocessing import Process,Queue import time,random,os def consumer(q,p1): while True: res=q.get() if(res=="finish"): break time.sleep(r 阅读全文
posted @ 2020-12-26 16:09 anobscureretreat 阅读(103) 评论(0) 推荐(0) 编辑
摘要: 并行:并行是指两者同时执行,比如赛跑,两个人都在不停的往前跑;(资源够用,比如三个线程,四核的CPU) 并发:并发是指资源有限的情况下,两者交替轮流使用资源,比如一段路(单核CPU资源)同时只能过一个人,A走一段后,让给B,B用完继续给A,交替使用,目的是提高效率。 并行:是从微观上,也就是在一个精 阅读全文
posted @ 2020-12-26 15:59 anobscureretreat 阅读(160) 评论(0) 推荐(0) 编辑
摘要: code import os from multiprocessing import Process def f(x): print('子进程id :',os.getpid(),'父进程id :',os.getppid()) return x*x if __name__ == '__main__': 阅读全文
posted @ 2020-12-26 15:55 anobscureretreat 阅读(70) 评论(0) 推荐(0) 编辑
摘要: 多进程并行 code import time from multiprocessing import Process def f(name): print('hello', name) time.sleep(1) if __name__ == '__main__': p_lst = [] for i 阅读全文
posted @ 2020-12-26 15:52 anobscureretreat 阅读(120) 评论(0) 推荐(0) 编辑
摘要: code import os from multiprocessing import Process class MyProcess(Process): def __init__(self,name): super().__init__() self.name=name def run(self): 阅读全文
posted @ 2020-12-26 15:42 anobscureretreat 阅读(75) 评论(0) 推荐(0) 编辑
摘要: code # 由并发变成了串行,牺牲了运行效率,但避免了竞争 import os import time import random from multiprocessing import Process,Lock def work(lock,n): lock.acquire() print('%s 阅读全文
posted @ 2020-12-26 15:03 anobscureretreat 阅读(270) 评论(0) 推荐(0) 编辑
摘要: 在并发编程中使用生产者和消费者能够解决绝大多数并发问题。该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度。 4.1什么是生产者消费者模式 生产者消费者模式是通过一个容器来解决生产者和消费者的强耦合问题。生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯,所以生产者生产 阅读全文
posted @ 2020-12-26 15:02 anobscureretreat 阅读(79) 评论(0) 推荐(0) 编辑
摘要: code import os import time import multiprocessing # 向Queue中输入数据的函数 def input(queue): info = str(os.getpid()) + '(put):' + str(time.asctime()) queue.pu 阅读全文
posted @ 2020-12-26 14:56 anobscureretreat 阅读(198) 评论(0) 推荐(0) 编辑
摘要: code # 文件db的内容为:{"count":1} # 注意一定要用双引号,不然json无法识别 # 并发运行,效率高,但竞争写同一文件,数据写入错乱 from multiprocessing import Process,Lock import time,json,random def sea 阅读全文
posted @ 2020-12-26 14:50 anobscureretreat 阅读(90) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 ··· 68 下一页