zoukankan      html  css  js  c++  java
  • python 并发编程入门

    多进程

    在Unix/Linux下,为我们提供了类似c中<unistd.h>头文件里的的fork()函数的接口,这个函数位于os模块中,相同与c中类似,对于父进程fork()调用返回子进程ID,对于子进程返回0

    import os, time
    
    pid = os.fork()
    if pid == 0:
        while True:
            print 'child process'
            time.sleep(1)
    else:
        while True:
            print 'parent process'
            time.sleep(3)

    考虑到Windows并没有这个调用,python为我们提供了跨平台的版本号。这就是multiprocessing模块。通过multiprocessing模块中的Process类可实现跨平台的多进程。使用方法很easy

    #coding:utf-8
    from multiprocessing import Process
    import os, time
    
    def handler(args):
    	print 'process parameter is %s' % args
    	while True:
    		print 'child process'
    		time.sleep(1)
    
    if __name__=='__main__':
        print 'parent process is %d' % os.getpid()
        child_proc = Process(target = handler, args=('test parameter',))   #指定子进程開始运行的函数
        child_proc.start()
        while True:
            print 'parent process'
            time.sleep(3)


    注意:若不加if __name__=='__main__'。子进程启动后会将模块内的代码再运行一遍。为避免不必要的错误,应该加上它

    Python为了更方便的使用多进程还提供了进程池Pool, 位于multiprocessing模块中,进程池用于对于并发响应要求较高的条件中,预先分配进程,节省了处理过程中fork的开销
    关于很多其它进程池的内容可參考 http://blog.csdn.net/aspnet_lyc/article/details/38946915#t3  中的TCP预先派生子进程server

    #coding:utf-8
    from multiprocessing import Pool
    import os, time, random
    
    def handler(proc_args):
        print proc_args
    
    if __name__ == '__main__':
        pool = Pool(4)                     		 #设置进程池中的进程数
        for loop in range(4):
    	pool.apply_async(handler, args=(loop,))  #apply_async(func,args),从进程池中取出一个进程运行func,args为func的參数。返回一个 AsyncResult的对象。对该对象调用get()方法能够获得结果。

    pool.close() #不在往进程池中加入进程 pool.join() #等待全部子进程结束 print 'All child processes done'


    多线程


    Python中对于多线程提供了thread和threading模块, threading对thread进行了封装,更易用。python官网的描写叙述例如以下

    This module provides low-level primitives for working with multiple threads (also called light-weight processes or tasks) — multiple threads of control sharing their global data space. For synchronization, simple locks (also called mutexes or binary semaphores) are provided. The threading module provides an easier to use and higher-level threading API built on top of this module

    调用thread模块中的start_new_thread()函数来产生新线程

    import thread
    
    def thread_handler(args):
        print args
    
    if __name__ == '__main__':
        thread.start_new_thread(thread_handler, ('test parameter',))
        while True:
            pass


     

    将线程函数传入并创建Thread实例。然后调用start()创建线程并运行

    import threading
    
    def thread_handler(args):
        print args
    
    if __name__ == '__main__':
        th1 = threading.Thread(target=thread_handler, args=('test parameter 1',))
        th2 = threading.Thread(target=thread_handler, args=('test parameter 2',))
        th1.start()
        th2.start()
        th1.join()
        th2.join()
        print 'All threads ended'


     



     

  • 相关阅读:
    C#+API实现指定窗体激活
    DEVC++学习之(一)
    javascript 实现原生下载的各种情况
    IssueVision 之WebService安全篇
    Add relationship to BS sample
    ExpandRelationWithCtxt 与 GetRelatedObjects 的区别
    C#调用javascript
    解禁网页限制
    Unix cc options vs gcc options
    IssueVision 之模式篇
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/6995244.html
Copyright © 2011-2022 走看看