python学习 day14 进程和线程
Posted afly
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习 day14 进程和线程相关的知识,希望对你有一定的参考价值。
进程
# *********通过process创建单个进程 from multiprocessing import Process import os # 子进程要执行的代码 def run_proc(name): print(‘Run child process %s (%s)...‘ % (name, os.getpid())) # 获取当前进程的名字,pid值 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 9976. # Child process will start. # Run child process test (9368)... # Child process end. # *************通过Pool创建多个进程,Pool内的参数为最多同时执行的进程数 from multiprocessing import Pool import os, time, random def long_time_task(name): print(‘Run task %s (%s)...‘ % (name, os.getpid())) start = time.time() time.sleep(random.random() * 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.‘) # Parent process 8808. # Waiting for all subprocesses done... # Run task 0 (2956)... # Run task 1 (5296)... # Run task 2 (8432)... # Run task 3 (8412)... # Task 3 runs 0.10 seconds. # Run task 4 (8412)... # Task 2 runs 0.41 seconds. # Task 4 runs 1.61 seconds. # Task 1 runs 1.90 seconds. # Task 0 runs 2.09 seconds. # All subprocesses done. #****************** subprocess可以让我们方便的启动一个子进程,然后控制其输入和输出,nslookup是终端下的一个命令,能查到指定网址的ip import subprocess print(‘$ nslookup www.python.org‘) r = subprocess.call([‘nslookup‘, ‘www.python.org‘]) # 创建了一个子进程并输入了 nslookup www.python.org print(‘Exit code:‘, r) ‘‘‘ ‘‘‘ #************** 如果子进程还需要输入,则可以通过communicate()方法输入 import subprocess print(‘$ nslookup‘) p = subprocess.Popen([‘nslookup‘], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output, err = p.communicate(b‘set q=mx python.org exit ‘) print(output.decode(‘gbk‘)) # print(output.decode(‘utf-8‘)) 源码是utf-8 但是会报错,看底下的评论是编码问题,换成gbk就好了 # 因为pycharm默认的是utf-8,这也导致了上面的例子有乱码, ‘$ nslookup www.baidu.com‘相当于是运行在cmd中的命令,命令运行后显示的结果也是cmd中的结果, # 而cmd本身默认编码是GBK的,所以会出现乱码。解决方法:File>>Settings>>File Encodings>>Project Encoding:(选择GBK) print(‘Exit code:‘, p.returncode) # 上面的代码相当于在命令行执行命令nslookup,然后手动输入: # set q=mx # python.org # exit # 输出结果如下 # $ nslookup # Server: 192.168.19.4 # Address: 192.168.19.4#53 # # Non-authoritative answer: # python.org mail exchanger = 50 mail.python.org. # # Authoritative answers can be found from: # mail.python.org internet address = 82.94.164.166 # mail.python.org has AAAA address 2001:888:2000:d::a6 # # # Exit code: 0 #*************进程间通信 from multiprocessing import Process, Queue import os, time, random # 写数据进程执行的代码: def write(q): print(‘Process to write: %s‘ % os.getpid()) for value in [‘A‘, ‘B‘, ‘C‘]: print(‘Put %s to queue...‘ % value) q.put(value) time.sleep(random.random()) # 读数据进程执行的代码: def read(q): # 通过q 传入Queue() 队列 这样就可以保存数据了 print(‘Process to read: %s‘ % os.getpid()) while True: value = q.get(True) # 这里的True其实是一个参数(block=True) ,表示当queue中没有数据的时候阻塞 print(‘Get %s from queue.‘ % value) if __name__==‘__main__‘: # 父进程创建Queue,并传给各个子进程: q = Queue() pw = Process(target=write, args=(q,)) pr = Process(target=read, args=(q,)) # 启动子进程pw,写入: pw.start() # 启动子进程pr,读取: pr.start() # 等待pw结束: pw.join() # pr进程里是死循环,无法等待其结束,只能强行终止: pr.terminate()
以上是关于python学习 day14 进程和线程的主要内容,如果未能解决你的问题,请参考以下文章