进程与线程的标识

知识点一:进程id 与 线程ident

import time
import multiprocessing
import threading
time.sleep(10)
print(multiprocessing.current_process().pid) # 进程 def func():
time.sleep(5) p = threading.Thread(target=func)
p.start()
print(p.ident) # 线程

xshell

ps aux | grep python3

知识点二:进程名 与 线程名

import time
import multiprocessing
import threading def func():
time.sleep(1) p = threading.Thread(target=func, name='李木子')
p.start()
print(p)
print(p.name) print('- - ' *20)
p.name = '李希'
print(p)

知识点三:获取当前线程/进程信息

提示!
可以在任何一个进程或者线程里搞清楚自己是谁

import time
import multiprocessing
print(multiprocessing.current_process()) # 获取主进程中的当前进程对象 def fun():
print("- - "*10)
print(multiprocessing.current_process()) # 获取子进程中的当前进程对象 p = multiprocessing.Process(target=fun, name='李希一号')
print(p)
p.start()

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

知识点一:等待结束

等待进程或线程结束

提示!进程和线程是一样的同学们课后自己尝试

import time
import multiprocessing print("外层start", time.asctime()) def func():
print("里层start", time.asctime())
time.sleep(5)
print("里层end", time.asctime())
p = multiprocessing.Process(target=func)
p.start()
time.sleep(5)
print("外层end", time.asctime())

可以看到,并行结果,主子同时进行

加上join,等待子进程结束,再执行主进程

import time
import multiprocessing print("外层start", time.asctime()) def func():
print("里层start", time.asctime())
time.sleep(5)
print("里层end", time.asctime())
p = multiprocessing.Process(target=func)
p.start() p.join() # 主进程 等待子进程结束 time.sleep(5)
print("外层end", time.asctime())

中止进程

注意!线程并不能被中途中止只能等待其运行结束

import time
import multiprocessing
print(multiprocessing.current_process()) def fun():
print("里层start", time.asctime())
time.sleep(5)
print("里层end", time.asctime()) p = multiprocessing.Process(target=fun, name='李希一号')
p.start()
time.sleep(2)
p.terminate() # 不管你执行多久,同时结束主进行和子进程

进程与线程的生存与状态

import time
import multiprocessing def func():
time.sleep(5) p = multiprocessing.Process(target=func, name='李木子')
print(p)
print("状态:",p.is_alive()) p.start()
print(p)
print("状态:",p.is_alive())

守护模式

提示!多线程中的守护线程与守护进程类似

通俗的讲,皇帝死后(主进程),妃子陪葬(子进程)

import time
import multiprocessing def func():
print('start')
time.sleep(5)
print('end') p = multiprocessing.Process(target=func, daemon=True) # 设置守护进程
p.start()
time.sleep(2)

以面向对象的形式使用进程与线程

面向对象使用线程/进程
步骤一: 继承 Process或Thread 类
步骤二: 重写 __init__方法
步骤三: 重写 run方法

import time
import multiprocessing class MyProcess(multiprocessing.Process): def __init__(self, *args, **kwargs):
super().__init__() # super重写
self.args = args
self.kwargs = kwargs def run(self):
print('我是重写的')
print(multiprocessing.current_process()) # 子进程的当前进程对象
print(self.args)
print(self.kwargs) print(multiprocessing.current_process()) # 主进程的当前进程对象
p = MyProcess(1, 2, 3, a=1, b=2) # 实例化
p.start()

面向对象使用的思路

用面向对象的方式来实现多线程并发服务器
import time
import socket
import threading server = socket.socket()
server.bind(('0.0.0.0', 8888))
server.listen() class MyThread(threading.Thread): def __init__(self, conn):
super().__init__() # super重写
self.conn = conn
print(self.conn) def run(self):
while True:
data = self.conn.recv(1024) if data == b'':
self.conn.close()
break
else:
print("接收到的消息: {}".format(data.decode()))
self.conn.send(data) while True:
conn, addr = server.accept()
print("{}正在连接".format(addr)) p = MyThread(conn) # 实例化
p.start()

客户端:

import socket

click = socket.socket()
click.connect(('127.0.0.1', 8888)) while True:
data = input("请输入你要发送的数据:")
click.send(data.encode())
print("接收到的消息: {}".format(click.recv(1024).decode()))

总结完毕!

作者:含笑半步颠√

博客链接:https://www.cnblogs.com/lixy-88428977

声明:本文为博主学习感悟总结,水平有限,如果不当,欢迎指正。如果您认为还不错,欢迎转载。转载与引用请注明作者及出处。

python_进程与线程的补充的更多相关文章

  1. Python_进程、线程及协程

    一.Python进程 IO密集型----多线程 计算密集型----多进程 1.单进程 from multiprocessing import Process def foo(i): print('你好 ...

  2. Python_进程process 与 线程thread

    进程process  与 线程thread 的区别 各个进程独立使用内存空间,(默认)不可互相访问,线程共享内存 进程的子进程是复制一份父进程,线程没有

  3. java并发编程:进程和线程

    java并发编程涉及到很多内容,当然也包括多线程,再次补充点相关概念 原文地址:http://www.cnblogs.com/dolphin0520/p/3910667.html 一.操作系统中为什么 ...

  4. Python-Cpython解释器支持的进程与线程-Day9

    Cpython解释器支持的进程与线程 阅读目录 一 python并发编程之多进程 1.1 multiprocessing模块介绍 1.2 Process类的介绍 1.3 Process类的使用 1.4 ...

  5. concurrent.futures模块(进程池/线程池)

    需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...

  6. top命令查看进程下线程信息以及jstack的使用

    转自:https://www.cnblogs.com/shengulong/p/8513652.html top -Hp pid可以查看某个进程的线程信息 -H 显示线程信息,-p指定pid jsta ...

  7. python---基础知识回顾(十)进程和线程(进程)

    前戏:进程和线程的概念 若是学过linux下的进程,线程,信号...会有更加深刻的了解.所以推荐去学习下,包括网络编程都可以去了解,尤其是对select,poll,epoll都会有更多的认识. 进程就 ...

  8. Python之路【第十一篇】: 进程与线程

    阅读目录 一. cpython并发编程之多进程1.1 multiprocessing模块介绍1.2 Process类的介绍1.3 Process类的使用1.4 进程间通信(IPC)方式一:队列1.5 ...

  9. windows中的进程和线程

    今天咱们就聊聊windows中的进程和线程 2016-09-30 在讨论windows下的进程和线程时,我们先回顾下通用操作系统的进程和线程.之所以称之为通用是因为一贯的本科或者其他教材都是这么说的: ...

随机推荐

  1. 《Java虚拟机JVM故障诊断与性能优化》读书笔记(未完待续)

    前言: 对于JVM学习用处的理解:我们程序员写的代码,虽说是放在服务器(linux)系统上的.但是很多时候,受JVM的影响,其实程序并没有发挥出服务器的最大性能.这时候,JVM就成为了瓶颈了.有瓶颈就 ...

  2. kernel 获取ntoskrnl.exe基址

    标题: kernel shellcode之寻找ntoskrnl.exe基址 http://scz.617.cn:8/windows/201704171416.txt 以64-bits为例,这是Eter ...

  3. 花了两个星期,我终于把 WSGI 整明白了

    在 三百六十行,行行转 IT 的现状下,很多来自各行各业的同学,都选择 Python 这门胶水语言做为踏入互联网大门的第一块敲门砖,在这些人里,又有相当大比例的同学选择了 Web 开发这个方向(包括我 ...

  4. shell 换行与不换行

    test.sh: echo -e "hello w\norld!"echo -e "hello w\c"echo "orld!" 输出 bo ...

  5. myeclipse的安装与破解

    myeclipe安装和破解一直困扰我很长时间,我又是尴尬症的人,不破解就是不行,花费一天时间终于搞定是怎么破解的. 一:首先myeclipse的官方下载网站www.myeclipsecn.com/do ...

  6. IntelliJ IDEA 2019从入门到癫狂 图文教程!

    阅读本文大概需要 6 分钟. 作者:yizhiwazi 来源:www.jianshu.com/p/9c65b7613c30 前言:IntelliJ IDEA 如果说IntelliJ IDEA是一款现代 ...

  7. Elasticsearch Java Rest Client简述

    ESJavaClient的历史 JavaAPI Client 优势:基于transport进行数据访问,能够使用ES集群内部的性能特性,性能相对好 劣势:client版本需要和es集群版本一致,数据序 ...

  8. HTML5中使用EventSource实现服务器发送事件

    在HTML5的服务器发送事件中,使用EventSource对象可以接收服务器发送事件的通知. 示例: es.html <!DOCTYPE html> <html> <he ...

  9. XML 中 5 个预定义的实体引用

    < < 小于 > > 大于 & & 和号 &apos; ' 省略号 " " 引号

  10. pytharm里面的导入上级目录飘红

    有时候导入本地模块或者py文件时,下方会出现红色的波浪线,但不影响程序的正常运行,但是在查看源函数文件时,会出现问题 问题如下: 解决方案: 1. 进入设置,找到Console下的Python Con ...