zoukankan      html  css  js  c++  java
  • python之线程与进程

    多任务的实现有3种方式:

    • 多进程模式;
    • 多线程模式;
    • 多进程+多线程模式。

    多进程

     Unix/Linux操作系统提供了一个fork()系统调用,它非常特殊。普通的函数调用,调用一次,返回一次,但是fork()调用一次,返回两次,因为操作系统自动把当前进程(称为父进程)复制了一份(称为子进程),然后,分别在父进程和子进程内返回。子进程永远返回0,而父进程返回子进程的ID。

     multiprocessing模块就是跨平台版本的多进程模块

    # from multiprocessing import Process
    # import os
    # def run_proc(name):
    #     print('Run child process %s (%s)...' % (name, os.getpid()))#用.getpid()就可以拿到父进程的ID
    # if __name__=='__main__':#生成进程必须要有
    #     print('Parent process %s.' % os.getpid())#用.getpid()就可以拿到子进程的ID
    #     p = Process(target=run_proc, args=('ww',)) #生成一个进程  target=run_proc调用函数, args=('test',)为函数添加参数
    #     print('Child process will start.')
    #     p.start()#开始运行子进程
    #     p.join()#等待子进程运行结束
    #     print('Child process end.')

    Pool

    如果要启动大量的子进程,可以用进程池的方式批量创建子进程

    # from multiprocessing import Pool
    # import os, time, random
    # def long_time_task(name):
    #     print('Run task %s (%s)...' % (name, os.getpid()))#用.getpid()就可以拿到父进程的ID
    #     start = time.time()#进程开始时间
    #     time.sleep(random.random() * 3)#休眠(随机数*3)秒
    #     end = time.time()#进程结束时间
    #     print('Task %s runs %0.2f seconds.' % (name, (end - start)))
    # if __name__=='__main__':
    #     print('Parent process %s.' % os.getpid())
    #     p = Pool(4)#允许同时运行的进程
    #     for i in range(5):
    #         p.apply_async(long_time_task, args=(i,))#生成进程
    #     print('Waiting for all subprocesses done...')
    #     p.close()#开始运行子进程
    #     p.join()#等待运行的子进程结束
    #     print('All subprocesses done.')

     多线程

     进程是由若干线程组成的,一个进程至少有一个线程。

    Python的线程是真正的Posix Thread,而不是模拟出来的线程。

    # import time, threading
    # # 新线程执行的代码:
    # def loop():
    #     print('thread %s is running...' % threading.current_thread().name)#threading模块有个current_thread(),永远返回当前线程的实例,主线程实例的名字叫MainThread
    #     n = 0
    #     while n < 5:
    #         n = n + 1
    #         print('thread %s >>> %s' % (threading.current_thread().name, n))
    #         time.sleep(1)#使线程休眠1秒
    #     print('thread %s ended.' % threading.current_thread().name)
    # print('thread %s is running...' % threading.current_thread().name)
    # t = threading.Thread(target=loop, name='LoopThread')#生成一个线程
    # t.start()#开始子线程
    # t.join()#等待子线程结束
    # print('thread %s ended.' % threading.current_thread().name)

  • 相关阅读:
    HDOJ 2955
    SG函数
    关于背包问题的二进制优化
    一个还算能用的调试代码
    ural 1568 Train Car Sorting 题解
    【虚拟机】:"该虚拟机似乎正在使用中。 如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权。否则,请按“取消(C)”按钮以防损坏。"
    解决Zookeeper报错:conf is not executed because it is not in the whitelist的解决办法
    Kali Linux更新和配置
    Docker拉取镜像时错误解决办法
    运行连接Oracle数据库时,Idea报错: Error : java 不支持发行版本5
  • 原文地址:https://www.cnblogs.com/wbf980728/p/14090282.html
Copyright © 2011-2022 走看看