Python学习之进程和线程

发表于:2018-6-26 09:58

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:stonezhu    来源:51testing软件测试网采编

  对于操作系统来说,一个任务就是一个进程(Process),比如打开一个浏览器就是启动一个浏览器进程,打开一个记事本就启动了一个记事本进程,打开两个记事本就启动了两个记事本进程,打开一个Word就启动了一个Word进程。
  有些进程还不止同时干一件事,比如Word,它可以同时进行打字、拼写检查、打印等事情。在一个进程内部,要同时干多件事,就需要同时运行多个“子任务”,我们把进程内的这些“子任务”称为线程(Thread)。
  进程
  Python的os模块封装了常见的系统调用,其中包括fork,可以在Python程序中轻松创建子进程:
  import os
  print('Process (%s) start...' % os.getpid())
  # Only works on Unix/Linux/Mac:
  pid = os.fork()
  if pid == 0:
  print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid()))
  else:
  print('I (%s) just created a child process (%s).' % (os.getpid(), pid))
  运行结果如下:
  Process (876) start...
  I (876) just created a child process (877).
  I am child process (877) and my parent is 876.
  由于Windows没有fork调用,上面的代码在Windows上无法运行。由于Mac系统是基于BSD(Unix的一种)内核,所以,在Mac下运行是没有问题的,推荐大家用Mac学Python!
  multiprocessing
  如果你打算编写多进程的服务程序,Unix/Linux无疑是正确的选择。由于Windows没有fork调用,难道在Windows上无法用Python编写多进程的程序?
  由于Python是跨平台的,自然也应该提供一个跨平台的多进程支持。multiprocessing模块就是跨平台版本的多进程模块。
  multiprocessing模块提供了一个Process类来代表一个进程对象,下面的例子演示了启动一个子进程并等待其结束:
  from multiprocessing import Process
  import os
  # 子进程要执行的代码
  def run_proc(name):
  print('Run child process %s (%s)...' % (name, os.getpid()))
  if __name__=='__main__':
  print('Parent process %s.' % os.getpid())
  p = Process(target=run_proc, args=('test',))
  print('Child process will start.')
  p.start()
  p.join()
  print('Child process end.')
  执行结果如下:
  Parent process 928.
  Process will start.
  Run child process test (929)...
  Process end.
  创建子进程时,只需要传入一个执行函数和函数的参数,创建一个Process实例,用start()方法启动,这样创建进程比fork()还要简单。
  join()方法可以等待子进程结束后再继续往下运行,通常用于进程间的同步。
  线程
  Python的标准库提供了两个模块:_thread和threading,_thread是低级模块,threading是高级模块,对_thread进行了封装。绝大多数情况下,我们只需要使用threading这个高级模块。
  启动一个线程就是把一个函数传入并创建Thread实例,然后调用start()开始执行:
  import time, threading
  # 新线程执行的代码:
  def loop():
  print('thread %s is running...' % threading.current_thread().name)
  n = 0
  while n < 5:
  n = n + 1
  print('thread %s >>> %s' % (threading.current_thread().name, n))
  time.sleep(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)
  执行结果如下:
  thread MainThread is running...
  thread LoopThread is running...
  thread LoopThread >>> 1
  thread LoopThread >>> 2
  thread LoopThread >>> 3
  thread LoopThread >>> 4
  thread LoopThread >>> 5
  thread LoopThread ended.
  thread MainThread ended.
  Lock
  多线程和多进程最大的不同在于,多进程中,同一个变量,各自有一份拷贝存在于每个进程中,互不影响,而多线程中,所有变量都由所有线程共享,所以,任何一个变量都可以被任何一个线程修改,因此,线程之间共享数据最大的危险在于多个线程同时改一个变量,把内容给改乱了。
  balance = 0
  lock = threading.Lock()
  def run_thread(n):
  for i in range(100000):
  # 先要获取锁:
  lock.acquire()
  try:
  # 放心地改吧:
  change_it(n)
  finally:
  # 改完了一定要释放锁:
  lock.release()
  当多个线程同时执行lock.acquire()时,只有一个线程能成功地获取锁,然后继续执行代码,其他线程就继续等待直到获得锁为止。
  获得锁的线程用完后一定要释放锁,否则那些苦苦等待锁的线程将永远等待下去,成为死线程。所以我们用try...finally来确保锁一定会被释放。
  ThreadLocal
  import threading
  # 创建全局ThreadLocal对象:
  local_school = threading.local()
  def process_student():
  # 获取当前线程关联的student:
  std = local_school.student
  print('Hello, %s (in %s)' % (std, threading.current_thread().name))
  def process_thread(name):
  # 绑定ThreadLocal的student:
  local_school.student = name
  process_student()
  t1 = threading.Thread(target= process_thread, args=('Alice',), name='Thread-A')
  t2 = threading.Thread(target= process_thread, args=('Bob',), name='Thread-B')
  t1.start()
  t2.start()
  t1.join()
  t2.join()
  执行结果:
  Hello, Alice (in Thread-A)
  Hello, Bob (in Thread-B)
  全局变量local_school就是一个ThreadLocal对象,每个Thread对它都可以读写student属性,但互不影响。你可以把local_school看成全局变量,但每个属性如local_school.student都是线程的局部变量,可以任意读写而互不干扰,也不用管理锁的问题,ThreadLocal内部会处理。
  可以理解为全局变量local_school是一个dict,不但可以用local_school.student,还可以绑定其他变量,如local_school.teacher等等。
  ThreadLocal最常用的地方就是为每个线程绑定一个数据库连接,HTTP请求,用户身份信息等,这样一个线程的所有调用到的处理函数都可以非常方便地访问这些资源。


上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号