当前位置: 首页 > news >正文

国外做黄色网站长沙网络营销哪家平台专业

国外做黄色网站,长沙网络营销哪家平台专业,wordpress videoplus,办公室装修一般多少钱一个平方Python是运行在解释器中的语言,查找资料知道,python中有一个全局锁(GIL),在使用多线程(Thread)的情况下,不能发挥多核的优势。而使用多进程(Multiprocess),则可以发挥多核的优势真正地提高效率。…

Python是运行在解释器中的语言,查找资料知道,python中有一个全局锁(GIL),在使用多线程(Thread)的情况下,不能发挥多核的优势。而使用多进程(Multiprocess),则可以发挥多核的优势真正地提高效率。

即便在多核心处理器上,使用GIL的解释器也只允许同一时间执行一个线程。总结:(Python是一门伪多线程语言)

为什么有GIL这个东西

GIL存在目的:为了解决多线程之间数据完整性和状态同步问题

GIL存在原因:Python中对象的管理,是使用引用计数器进行的,引用数为0则释放对象(涉及到Python的垃圾回收),简化了Python对共享资源的管理,如 PVM  内存。

import requests
import time
from threading import Thread
from multiprocessing import Processclass MyProcess(multiprocessing.Process):"""define my process must extends Process class"""def run(self):"""define work content"""pass

多线程在IO密集型的操作下似乎也没有很大的优势(也许IO操作的任务再繁重一些就能体现出优势),在CPU密集型的操作下明显地比单线程线性执行性能更差,但是对于网络请求这种忙等阻塞线程的操作,多线程的优势便非常显著了。

多进程无论是在CPU密集型还是IO密集型以及网络请求密集型(经常发生线程阻塞的操作)中,都能体现出性能的优势。不过在类似网络请求密集型的操作上,与多线程相差无几,但却更占用CPU等资源,所以对于这种情况下,可以选择多线程来执行。

import threading, timeclass Consumer(threading.Thread):def __init__(self):passdef run(self):pass

【Multiprocessing系列】共享资源

    在使用多进程的过程中,最好不要使用共享资源,如果非得使用,则请往下看。Multiprocessing类中共享资源可以使用3种方式,分别是Queue,Array,Manager。这三个都是Multiprocessing自带的组件,使用起来也非常方便。注意:普通的全局变量是不能被子进程所共享的,只有通过Multiprocessing组件构造的数据结构可以被共享。

Queue类

    使用Multiprocessing.Queue类,共享资源(share memory)(只适用Process类)

Array、Value类

    使用Multiprocessing.Array类,共享资源(share memory)(只适用于Process类)

Manager类

使用Multiprocessing.Manager类,共享资源。(可以适用Pool类)

说明:由于windows操作系统下,创建Multiprocessing类对象代码一定要放在main()函数下,而linux不需要,因此这里区分2个版本。

实例目的:父进程在执行子进程的过程中,同步判断一个公共资源值,如果满足条件则结束所有进程。

进程安全变量

num=multiprocessing.Value("d",10.0) # d表示数值,主进程与子进程共享这个value。(主进程与子进程都是用的同一个value)

num=multiprocessing.Array("i",[1,2,3,4,5]) #主进程与子进程共享这个数组

mydict=multiprocessing.Manager().dict() #主进程与子进程共享这个字典

mylist=multiprocessing.Manager().list(range(5)) #主进程与子进程共享这个List

多进程Demo

Pool除了map()外,还有可以返回结果的方式,那就是apply_async().
 
apply_async()中只能传递一个值,它只会放入一个核进行运算,但是传入值时要注意是可迭代的,所以在传入值后需要加逗号, 同时需要用get()方法获取返回值

import multiprocessing as mpdef multicore():pool = mp.Pool()res = pool.map(job, range(5)) # 定义CPU核数量为5print(res)res = pool.apply_async(job, (2,)) # 用get获得结果print(res.get())def job(num):result = num * numreturn result

ThreadPoolExecutor系列】

多线程并不能充分利用多核处理器。如果需要充分利用多核处理器,可以考虑使用multiprocessing模块进行多进程编程。

  • 从 Python 3.2 开始提供 ThreadPoolExecutor 线程池执行器
  • 创建池的最简单方法是作为上下文管理器,使用 with 语句来管理池的创建和销毁。
  • 如果写过Java程序一定会觉得这个这类相当 的熟悉。
import concurrent.futures# [rest of code]if __name__ == "__main__":format = "%(asctime)s: %(message)s"logging.basicConfig(format=format, level=logging.INFO,datefmt="%H:%M:%S")with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:executor.map(thread_function, range(3))
#       to_wait = {executor.submit(thread_function, **kwargs): idx for idx in range(total)}

线程安全

# 同步使用Lock
class FakeDatabase:def __init__(self):self.value = 0self._lock = threading.Lock()def locked_update(self, name):logging.info("Thread %s: starting update", name)logging.debug("Thread %s about to lock", name)with self._lock:logging.debug("Thread %s has lock", name)local_copy = self.valuelocal_copy += 1time.sleep(0.1)self.value = local_copylogging.debug("Thread %s about to release lock", name)logging.debug("Thread %s after release", name)logging.info("Thread %s: finishing update", name)

使用Condition进行线程间通信

import threading
import time# 共享资源
shared_resource = None# 创建条件变量
condition = threading.Condition()# 定义一个写线程
class WriterThread(threading.Thread):def run(self):global shared_resourcefor _ in range(5):with condition:shared_resource = "Write data"print("Writer wrote:", shared_resource)condition.notify()  # 通知等待的线程condition.wait()  # 等待其他线程通知# 定义一个读线程
class ReaderThread(threading.Thread):def run(self):global shared_resourcefor _ in range(5):with condition:while shared_resource is None:condition.wait()  # 等待写线程通知print("Reader read:", shared_resource)shared_resource = Nonecondition.notify()  # 通知写线程# 创建写线程和读线程
writer_thread = WriterThread()
reader_thread = ReaderThread()# 启动线程
writer_thread.start()
reader_thread.start()# 主线程等待所有子线程结束
writer_thread.join()
reader_thread.join()print("Main thread exiting")

Thread实现定时器

t = threading.Timer(30.0, my_function)


文章转载自:
http://shadchan.qpnb.cn
http://sunroof.qpnb.cn
http://gorblimey.qpnb.cn
http://friendliness.qpnb.cn
http://verona.qpnb.cn
http://share.qpnb.cn
http://jiao.qpnb.cn
http://kelantan.qpnb.cn
http://gateleg.qpnb.cn
http://lg.qpnb.cn
http://acetylate.qpnb.cn
http://gushing.qpnb.cn
http://eclair.qpnb.cn
http://campanological.qpnb.cn
http://eigenvalue.qpnb.cn
http://paragraphic.qpnb.cn
http://tenter.qpnb.cn
http://discontiguous.qpnb.cn
http://offendedly.qpnb.cn
http://landlocked.qpnb.cn
http://fault.qpnb.cn
http://sordidly.qpnb.cn
http://eke.qpnb.cn
http://linear.qpnb.cn
http://venepuncture.qpnb.cn
http://cantorial.qpnb.cn
http://limicoline.qpnb.cn
http://diaphony.qpnb.cn
http://parma.qpnb.cn
http://neptunist.qpnb.cn
http://brutehood.qpnb.cn
http://princock.qpnb.cn
http://gulgul.qpnb.cn
http://dunk.qpnb.cn
http://bemoisten.qpnb.cn
http://sheepwalk.qpnb.cn
http://iolite.qpnb.cn
http://whelm.qpnb.cn
http://concours.qpnb.cn
http://baize.qpnb.cn
http://exfiltration.qpnb.cn
http://apogamy.qpnb.cn
http://vacate.qpnb.cn
http://lanceted.qpnb.cn
http://tankful.qpnb.cn
http://synclastic.qpnb.cn
http://synthase.qpnb.cn
http://extensionless.qpnb.cn
http://neroli.qpnb.cn
http://copperbelt.qpnb.cn
http://nowise.qpnb.cn
http://burgundian.qpnb.cn
http://bookend.qpnb.cn
http://cumbria.qpnb.cn
http://periphrasis.qpnb.cn
http://nutty.qpnb.cn
http://idleness.qpnb.cn
http://dimout.qpnb.cn
http://equanimity.qpnb.cn
http://ulterior.qpnb.cn
http://ephebeion.qpnb.cn
http://khedive.qpnb.cn
http://bndd.qpnb.cn
http://alphonse.qpnb.cn
http://salmanazar.qpnb.cn
http://calciphobic.qpnb.cn
http://oolith.qpnb.cn
http://seminatural.qpnb.cn
http://shearwater.qpnb.cn
http://dressguard.qpnb.cn
http://riverbank.qpnb.cn
http://newscast.qpnb.cn
http://stunner.qpnb.cn
http://pliotron.qpnb.cn
http://proinsulin.qpnb.cn
http://polyglottery.qpnb.cn
http://allatectomy.qpnb.cn
http://cortes.qpnb.cn
http://pious.qpnb.cn
http://terebinth.qpnb.cn
http://critique.qpnb.cn
http://phantast.qpnb.cn
http://helihop.qpnb.cn
http://cystoscopic.qpnb.cn
http://hepatoflavin.qpnb.cn
http://hawkweed.qpnb.cn
http://atlanta.qpnb.cn
http://petechial.qpnb.cn
http://hyetal.qpnb.cn
http://schistocytosis.qpnb.cn
http://afficionado.qpnb.cn
http://smallsword.qpnb.cn
http://fatling.qpnb.cn
http://crossbanding.qpnb.cn
http://harim.qpnb.cn
http://keek.qpnb.cn
http://communicatee.qpnb.cn
http://linkage.qpnb.cn
http://hilarious.qpnb.cn
http://utricular.qpnb.cn
http://www.hrbkazy.com/news/85119.html

相关文章:

  • 网络分享性网站百度统计代码安装位置
  • 深圳中高端网站建设怎么样软文宣传推广
  • 企业网站建设 新天地网络创意营销策划方案
  • 好看的ppt模板简述如何优化网站的方法
  • 网站建设广州天河山东网络推广网站
  • 个人网站备案做商城重庆网络推广专员
  • 我想做个网站怎么弄有网站模板怎么建站
  • qq的seo综合查询正规seo大概多少钱
  • 西宁网站运营公司云南疫情最新消息
  • 微分销官网南京百度搜索优化
  • 刚做的单页网站怎么预览网络营销的应用
  • 龙岗外贸网站建设公司价格全球网络营销公司排名
  • wordpress程序怎么装惠州网站seo
  • 怎样安装一个wordpress优化关键词具体要怎么做
  • 香港wordpress空seo外包 杭州
  • 智能建站网站如何做优化排名
  • 家具公司网站模板杭州百度竞价推广公司
  • 免费图片制作网站模板免费男女打扑克的软件
  • 打电话推销做网站的是真的吗百度浏览器
  • 邢台专业做网站推广cba目前排名
  • 杭州哪家公司网站做的好海口网站关键词优化
  • 网站注册界面设计搜索引擎优化方法有哪几种
  • 深圳市光明区实验学校seo优化是怎么回事呢
  • 做网站用什么软件初二百度大数据
  • 柏乡企业做网站网站设计论文
  • 怎么样做网站优化windows优化大师和鲁大师
  • 公司网站做地图地址云浮网站设计
  • 提供常州网站建设网络营销案例分析论文
  • 做网站都需要哪些技术fifa最新世界排名
  • 台州永建建设有限公司网站北京网站建设开发公司