潭州课堂25班:Ph201805201 并发(进程,线程)二 第十二课 (课堂笔记

线程与进程的其他相关操作

import threading            #  线程
import multiprocessing      #  进程
import socket
import time

def wo():
    global a
    time.sleep(5)
    a = 5

if __name__ == '__main__':              #    在 win  系统下
    p = multiprocessing.Process(target=wo)      #  生成进程
    print(p)                                      #  对象 名字 <Process(Process-1, initial)>
    p1 = multiprocessing.Process(target=wo, name='对象名字')      #  <Process(对象名字, initial)>
    p1.name = '改名字'                             #  可以改个名字   <Process(改名字, initial)>
    print(p1)                                      #  对象 名字
    # <Process(Process-1, initial)>
    # 可以指定名字
    # p = threading.Thread(target=wo)               #  生成线程
    # print('a:',p.pid)                           #  进程还没运行 : a: None
    print('a:',p.ident)                           #   线程还没运行  a: None
    p.start()                                   #  运行之后:
    # print('a:', p.pid)                          #  a: 12248  是进程 PID
    print('a:', p.ident)                          #   a: 24416  是线程 ident

#  在进程里 pid 是 ident, 在线程中没有 pid,只有 ident
#     线程与进程操作类似

  

import threading            #  线程
import multiprocessing      #  进程
import socket
import time

def wo():
    global a
    a = 5
    time.sleep(15)
    print('子进程结束')


if __name__ == '__main__':              #    在 win  系统下
    p = multiprocessing.Process(target=wo)
    p.start()
    time.sleep(2)
    p.join()                    #    主进程将停在这等待子进程 ,

    p.terminate()               #   主进程结束后,子进程立马关闭,未完成任务不再运行,
    # 但是线程没有这个方法,不能中途停止,只能等到他任务完成,

    print('主进程结束')

  

进程的生存状态

# -*- coding: utf-8 -*-
# 斌彬电脑
# @Time : 2018/7/18 0018 0:13
import threading            #  线程
import multiprocessing      #  进程
import socket
import time

def wo():
    global a
    a = 5
    time.sleep(15)



if __name__ == '__main__':              #    在 win  系统下
    p = multiprocessing.Process(target=wo)
    print(p)                                #  initial 对象生成后的初始化,<Process(Process-1, initial)>
    p.start()
    print(p)                                #  started 运行状态   <Process(Process-1, started)>
    print(p.is_alive())                     #  只有在运行中时才返回 True,

    p.join()                                #  等待任务完毕
    print(p)                                #  stopped 进程结束     <Process(Process-1, stopped)>

 线程与进程一样的操作,

守护进程 

import threading            #  线程
import multiprocessing      #  进程
import socket
import time

def wo():
    print('a')
    time.sleep(6)
    print('b')



if __name__ == '__main__':              #    在 win  系统下
    p = multiprocessing.Process(target=wo,daemon=True)
    p1 = multiprocessing.Process(target=wo)
    p1.daemon = True
    #   daemon=True  时,这个进程成为一个守护进程,
    #   只要运行完代码,直接关闭所有,

    p.start()
    print('c')

  

是为了程序完毕后释放被占到用的资源,( join()    daemon)

用面向对象的方式来使用进程和线程,

import threading            #  线程
import multiprocessing      #  进程
import socket
import time

class MyProcess(multiprocessing.Process):
    '''继承  multiprocessing.Process 这个类'''
    def __init__(self):             #  重写 init 方法
    # def __init__(self,name):             #  重写 init 方法
        super().__init__()          #   调用父类方法
        # super().__init__(name = name,daemon = True,)
        #  可以在这里设置参数 ,

    def run(self):                  #  重写 run 方法
        print(time.time())
        time.sleep(3)
        print(time.time())



if __name__ == '__main__':              #    在 win  系统下

    p = MyProcess()
    # p = MyProcess('abc')      #  传参
    p.start()                   #  启动时会自动调用 run 方法

  

  线程与进程一样操作,

原文地址:https://www.cnblogs.com/gdwz922/p/9326973.html