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

江西会昌建设局网站浏览器免费大全

江西会昌建设局网站,浏览器免费大全,凡客网站建站教程,vi设计公司当涉及到操作系统的时候#xff0c;免不了要使用os模块#xff0c;有时还要用到sys模块。 设计到并行程序#xff0c;一般开单独的进程#xff0c;而不是线程#xff0c;原因是python解释器的全局解释器锁GIL#xff08;global interpreter lock#xff09;#xff0c;… 当涉及到操作系统的时候免不了要使用os模块有时还要用到sys模块。 设计到并行程序一般开单独的进程而不是线程原因是python解释器的全局解释器锁GILglobal interpreter lock本文最后会讲到。使用进程可以实现完全并行无GIL的限制可充分利用多cpu多核的环境。   os/sys模块 1、os模块 os.system() 函数可以启动一个进程执行完之后返回状态码。 os.fork() 复制一个进程如果是子进程返回0如果是父进程返回子进程的pid使用这个函数的时候建议你学习一下linux编程的知识。 os.popen 以管道的方式创建进程。 os.spawnl 也可以创建进程并能指定环境变量。 os.kill(pid, sig) 关闭一个进程pid是进程号sig是信号。与fork配合使用例如你刚才用fork创建了一个子进程它的pid是11990 那么调用 os.kill( 11990, signal.CTRL_BREAK_EVENT) 就以ctrlc的方式杀死了这个进程。 os.wait() - (pid, status)找到任一个僵死子进程或者等待任一个子进程的SIGCHLD信号 os.waitpid(pid, options) - (pid, status) 等待给定进程结束   除了利用os模块实现多进程和通信还有一个模块multiprocessing封装了很多创建进程和进程间通信的操作发挥多核的威力。 附 文件操作也是与操作系统相关的操作也被封装在os模块。 文件操作的详细内容见  http://www.cnblogs.com/xinchrome/p/5011304.html   2、sys模块 同样是一个与系统相关的模块它们都表示了程序运行的上下文环境。但是与os模块不同的是os模块主要封装系统操作sys模块主要封装系统中的各种环境参数。 比如文件操作、进程线程操作封装在os模块中 标准输入输出stdout/stdin、命令行参数argv、环境变量path、平台platform等参数封装在sys模块中 不过sys中也含有一些进程操作比如sys.exit(n)和sys.exit(Unable to create first child.)   多进程multiprocessing  multiprocessing模块的内容 multiprocessing.Process(targetrun)类的实例表示一个进程具有字段 pid方法 start() join()等 multiprocessing.Pool(processes4) 类的实例表示一个进程池 multiprocessing.Lock类的实例表示一个锁具有acquire()和release() 方法 multiprocessing.Semaphore(2) 信号量类的实例表示一个信号量可以指定初始值具有 acquire() 和 release() 方法 multiprocessing.Event() 表示一个信号用于实现多进程等待某一个进程的情况 进程间要实现通信除了锁、信号量、事件还有队列multiprocessing.Queue。   import multiprocessingdef writer_proc(q): try: q.put(1, block False) except: pass def reader_proc(q): try: print q.get(block False) except: passif __name__ __main__:q multiprocessing.Queue()writer multiprocessing.Process(targetwriter_proc, args(q,)) writer.start() reader multiprocessing.Process(targetreader_proc, args(q,)) reader.start() reader.join() writer.join()   multiprocessing.Queue是多进程安全的队列可以使用Queue实现多进程之间的数据传递。put方法用以插入数据到队列中put方法还有两个可选参数blocked和timeout。如果blocked为True默认值并且timeout为正值该方法会阻塞timeout指定的时间直到该队列有剩余的空间。如果超时会抛出Queue.Full异常。如果blocked为False但该Queue已满会立即抛出Queue.Full异常。 get方法可以从队列读取并且删除一个元素。同样get方法有两个可选参数blocked和timeout。如果blocked为True默认值并且timeout为正值那么在等待时间内没有取到任何元素会抛出Queue.Empty异常。如果blocked为False有两种情况存在如果Queue有一个值可用则立即返回该值否则如果队列为空则立即抛出Queue.Empty异常。     进程同步互斥实例   import multiprocessing import time# 信号量实现同步进程间也可以使用信号量 def preq(s):print as.release()s.release()def worker(s,i):s.acquire()s.acquire()print(multiprocessing.current_process().name acquire)time.sleep(i)print(multiprocessing.current_process().name release)s.release() if __name__ __main__:s multiprocessing.Semaphore(0)for i in range(1):pre multiprocessing.Process(targetpreq, args(s,))pre.start()p multiprocessing.Process(targetworker, args(s,1))p.start()# 锁实现进程间对文件的互斥访问 def worker_with(lock, f):with lock:fs open(f,a)fs.write(Lock acquired via with\n)fs.close()def worker_no_with(lock, f):lock.acquire()try:fs open(f,a)fs.write(Lock acquired directly\n)fs.close()finally:lock.release() if __name__ __main__:f file.txtlock multiprocessing.Lock()w multiprocessing.Process(targetworker_with, args(lock, f))nw multiprocessing.Process(targetworker_no_with, args(lock, f))w.start()nw.start()w.join()nw.join()# Event实现同步Event类的实例表示一个信号进程调用它的wait方法等待被唤醒调用set方法唤醒所有正在等待的进程 def wait_for_event(e):Wait for the event to be set before doing anythingprint (wait_for_event: starting)e.wait()print (wait_for_event: e.is_set()- str(e.is_set())) def wait_for_event_timeout(e, t):Wait t seconds and then timeoutprint (wait_for_event_timeout: starting)e.wait(t)print (wait_for_event_timeout: e.is_set()- str(e.is_set()))if __name__ __main__:e multiprocessing.Event()w1 multiprocessing.Process(nameblock, targetwait_for_event,args(e,))w1.start()w2 multiprocessing.Process(namenon-block, targetwait_for_event_timeout, args(e, 2))w2.start()time.sleep(3)e.set()print (main: event is set)   Process类中定义的方法 | is_alive(self) | Return whether process is alive | | join(self, timeoutNone) | Wait until child process terminates | | run(self) | Method to be run in sub-process; can be overridden in sub-class | | start(self) | Start child process | | terminate(self) | Terminate process; sends SIGTERM signal or uses TerminateProcess() 以上来自于Python自带帮助   进程处理信号 signal  利用signal模块进程可以捕获信号根据相应的handler做处理。 信号signal-- 进程之间通讯的方式是一种软件中断。一个进程一旦接收到信号就会打断原来的程序执行流程来处理信号。 几个常用信号:     SIGINT 终止进程 中断进程 (controlc)     SIGQUIT 退出进程     SIGTERM 终止进程 软件终止信号 (命令行中输入kill命令时向进程发送的默认信号) 当直接写kill PID默认是向进程发送SIGTERM     SIGKILL 终止进程杀死进程捕捉这个信号会报错也就是进程不能捕捉此信号(kill -9)     SIGALRM 闹钟信号。Alarms信号是一个特殊信号类型它可以让程序要求系统经过一段时间对自己发送通知。os 标准模块中指出它可用于避免无限制阻塞 I/O 操作或其它系统调用。     SIGCHLD 子进程退出时对父进程发出的信号如果父进程还没有处理它子进程将会停留在僵死状态等待其父进程调用wait函数这个状态下的子进程就是僵死进程 PS常用信号简介 ctrl-c 发送 SIGINT 信号给前台进程组中的所有进程。常用于终止正在运行的程序。 ctrl-z 发送 SIGTSTP 信号给前台进程组中的所有进程常用于挂起一个进程。 ctrl-d 不是发送信号而是表示一个特殊的二进制值表示 EOF也就是输入流(例如普通文件或者stdin)的结束。 ctrl-\ 发送 SIGQUIT 信号给前台进程组中的所有进程终止前台进程并生成 core 文件。   常常会在python程序被关闭之前加一个钩子用atexit模块以及signal模块来实现 父进程捕获终止信号后还需要向每个子进程发送终止信号。 注意信号处理函数需要接受两个参数。   #!/usr/bin python# 正常退出或者被ctlc终止时进程捕获信号调用处理函数(钩子) import atexit from signal import signal, SIGTERMdef test():print exit........ atexit.register(test) signal(SIGTERM, lambda signum, stack_frame: exit(1))while True:pass# 进程发送信号终止其他进程 import os import signal #发送信号16175是前面那个绑定信号处理函数的pid需要自行修改 os.kill(16175,signal.SIGTERM) os.kill(16175,signal.SIGUSR1) # Linux编程范式fork()等待SIGCHLD信号 import os import signal from time import sleep def onsigchld(a,b): print 收到子进程结束信号 signal.signal(signal.SIGCHLD,onsigchld) pid os.fork() if pid 0: print 我是子进程,pid是,os.getpid() sleep(2) else: print 我是父进程,pid是,os.getpid() os.wait() #等待子进程结束 # 闹钟信号用于告诉操作系统向自己发送信号 import signal import timedef receive_alarm(signum, stack):print Alarm :, time.ctime()# Call receive_alarm in 2 seconds signal.signal(signal.SIGALRM, receive_alarm) signal.alarm(2)print Before:, time.ctime() time.sleep(10) print After :, time.ctime()     多线程threading thread   与进程不同线程要实现同步直接用Python自带的Queue模块即可。Python的Queue模块中提供了同步的、线程安全的队列类包括FIFO先入先出)队列QueueLIFO后入先出队列LifoQueue和优先级队列PriorityQueue。这些队列都实现了锁原语能够在多线程中直接使用。可以使用队列来实现线程间的同步。      python多线程编程一般使用thread和threading模块。thread模块想对较底层threading模块对thread模块进行了封装更便于使用。所有通常多线程编程使用threading模块。线程的创建一般有两种①将创建的函数传递进threading.Thread()对象的target字段可以是函数或者定义了__call__方法的类实例。②继承threading.Thread类通常重写run()方法。 1 threading模块的内容 Thread类的实例可以引用一个线程这是我们用的最多的一个类你可以指定目标线程函数执行或者自定义继承自它的子类都可以实现子线程功能 Timer类是Thread类的子类表示等待一段时间后才开始运行某段代码或者重复运行某段代码 (Python自带的)Queue类的实例是实现了多生产者Producer、多消费者Consumer的队列支持锁原语能够在多个线程之间提供很好的同步支持 Lock类的实例引用了一个锁原语这个我们可以对全局变量互斥时使用提供acquire和release方法 RLock的实例表示可重入锁使单线程可以再次获得已经获得的锁 Condition类的实例表示条件变量能让一个线程停下来等待其他线程满足某个“条件”除了提供acquire和release方法外还提供了wait和notify方法相当于一个多功能信号量 Event 类的实例表示通用的条件变量。多个线程可以等待某个事件发生在事件发生后所有的线程都被激活 Semaphore类的实例表示信号量为等待资源的线程提供一个类似队列的结构提供acquire和release方法初始化的时候可以指定初值Semaphore(3) BoundedSemaphore 与semaphore类似但不允许超过初始值    threading.Thread类的内容 getName(self) 返回线程的名字 isAlive(self) 布尔标志表示这个线程是否还在运行中 isDaemon(self) 返回线程的daemon标志 join(self, timeoutNone) 程序挂起直到线程结束如果给出timeout则最多阻塞timeout秒 run(self) 定义线程的功能函数 setDaemon(self, daemonic) 把线程的daemon标志设为daemonic setName(self, name) 设置线程的名字 start(self) 开始线程执行 ps:th.join()方法可能不是很安全如果th对应的线程没有被真正启动那么调用th.join()的线程将不会等待而会继续运行下去。用信号量更好。   多线程举例 #coding:utf-8 import threading, time #最简单的启动线程的方式 def sayHi(): time.sleep(1) print Hi, linuxapp ththreading.Thread(targetsayHi) th.start() th.join() # 使用threading.Thread()设置线程类实例的target属性表示一个线程 def T():print threading.current_thread().getName()t1 threading.Thread(targetT, namett11) t1.start() t1.join()# 通过threading.Thread类的子类实例表示线程注意父类构造方法__init__不能省略 class T2(threading.Thread):def __init__(self):threading.Thread.__init__(self)def run(self):print in run() of T2 threading.current_thread().getName()# threading.Lock类的实例表示一个互斥锁一个资源被加锁后其他线程不能访问 class T3(threading.Thread):def __init__(self):threading.Thread.__init__(self)self.counter 0;self.mutex threading.Lock()def run(self):time.sleep(1)if self.mutex.acquire():self.counter 1print self.counterself.mutex.release()# 如果同一个线程需要多次获得资源如果不使用 mutex threading.RLock() 就会死锁 class T4(threading.Thread):def __init__(self):threading.Thread.__init__(self)self.counter 0self.mutex threading.Lock()def run(self):time.sleep(1)if self.mutex.acquire():self.counter 1if self.mutex.acquire():self.counter 1self.mutex.release()self.mutex.release()def main():t T3()t.start()if __name__ __main__:main()# threading.Condition类的实例表示一个条件变量相当于多功能信号量 condition threading.Condition() products 0 class Producer(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): global condition, products while True: if condition.acquire(): if products 10: products 1; print Producer(%s):deliver one, now products:%s %(self.name, products) condition.notify() else: print Producer(%s):already 10, stop deliver, now products:%s %(self.name, products) condition.wait(); condition.release() time.sleep(2) class Consumer(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): global condition, products while True: if condition.acquire(): if products 1: products - 1 print Consumer(%s):consume one, now products:%s %(self.name, products) condition.notify() else: print Consumer(%s):only 1, stop consume, products:%s %(self.name, products) condition.wait(); condition.release() time.sleep(2) if __name__ __main__: for p in range(0, 2): p Producer() p.start() for c in range(0, 10): c Consumer() c.start()# threading.Event类的实例表示一个信号如果信号signal为true那么等待这个signal的所有线程都将可以运行 class MyThread(threading.Thread): def __init__(self, signal): threading.Thread.__init__(self) self.singal signal def run(self): print I am %s,I will sleep ...%self.name # 进入等待状态self.singal.wait() print I am %s, I awake... %self.name if __name__ __main__:# 初始 为 False singal threading.Event() for t in range(0, 3): thread MyThread(singal) thread.start() print main thread sleep 3 seconds... time.sleep(3) # 唤醒含有signal, 处于等待状态的线程 singal.set()   python多线程的限制 python多线程有个讨厌的限制全局解释器锁global interpreter lock这个锁的意思是任一时间只能有一个线程使用解释器跟单cpu跑多个程序也是一个意思大家都是轮着用的这叫“并发”不是“并行”。手册上的解释是为了保证对象模型的正确性这个锁造成的困扰是如果有一个计算密集型的线程占着cpu其他的线程都得等着试想你的多个线程中有这么一个线程多线程生生被搞成串行当然这个模块也不是毫无用处手册上又说了当用于IO密集型任务时IO期间线程会释放解释器这样别的线程就有机会使用解释器了所以是否使用这个模块需要考虑面对的任务类型。   转载自http://www.cnblogs.com/xinchrome/p/5031497.html
http://www.pierceye.com/news/486279/

相关文章:

  • 公司网站免费建设2023设计院裁员惨烈程度
  • 别人做的网站不能用设计网站教程
  • 设计师发布作品的网站wordpress仿
  • 品牌微信网站建设柳州做网站制作的公司有哪些
  • 买域名做网站推广都是些什么网站点击后的loading是怎么做的
  • 北京网站优化技术泰州自助建站软件
  • 公司企业网站建设目的站长统计官方网站
  • 集团公司网站模板wordpress更换主题方法
  • 临沂网站建设电话建设网站审批手续
  • 国外做健康的网站专门做鞋子的网站吗
  • 手机网站支持微信支付吗北京短视频拍摄
  • 做静态网站工资多少网站前期推广
  • 做预算查价格的网站是哪个好网站开发维护多少钱
  • 个人互动网站365建筑人才网
  • 天津外贸公司网站制作淘客cms网站建设
  • 怎样做微网站网站建设pc指什么软件
  • 四川 网站建设wordpress腾讯地图插件下载
  • 宁波网站公司哪家好百度关键词搜索量排名
  • 北京国税局网站做票种核定时眉山网站优化
  • 网站备案授权书成都网站建设十强企业
  • 网站流量图怎么做的wordpress单号管理系统
  • 生活服务网站建设方案天猫店铺装修做特效的网站
  • 公众号做视频网站会封吗开发微分销系统
  • 情侣博客网站模板下载kindeditor for wordpress
  • 广东网站备案进度查询长沙seo网络营销推广
  • 网站建设的一般过程包括哪些内容简单网页
  • 眉山市规划建设局网站专做网页的网站
  • 珠海网站建设开发ck网站
  • 医疗网站设计小程序开发制作费用
  • 德州网站建设网页设计实验报告总结