没有合适的资源?快使用搜索试试~ 我知道了~
多线程的总结知识 比较杂乱 设计各种概念和可以参考的链接文献 主要是给自己总结备用
资源推荐
资源详情
资源评论
1. python 主线程和子线程并发,切换的形式如下
import time
import threading
def thread_1(i):
time.sleep(2)
print("Active current thread right now:", (threading.current_thread()))
print('Value by Thread 1:', i)
def thread_2(i):
time.sleep(5)
print("Active current thread right now:", (threading.current_thread()))
print('Value by Thread 2:', i)
def thread_3(i):
print("Active current thread right now:", (threading.current_thread()))
print("Value by Thread 3:", i)
# Creating sample threads
thread1 = threading.Thread(target=thread_1, args=(1,))
thread2 = threading.Thread(target=thread_2, args=(2,))
thread3 = threading.Thread(target=thread_3, args=(3,))
Commented [F2]: When we run a Python
script, it starts an instance of the Python
interpreter that runs our code in the main
thread. The main thread is the default
thread of a Python process.
When we create and run a new thread,
Python will make system calls on the
underlying operating system and request
a new thread be created and to start
running the new thread.Python threads
are real threads.
threading.main_thread()
Return the main Thread object. In normal
conditions, the main thread is the thread
from which the Python interpreter was
started.
print("Active current thread right now:", (threading.current_thread()))
#3 Initially it is the main thread that is active
# Starting the threads
thread1.start()
thread2.start()
thread3.start()
output:
Active current thread right now: <_MainThread(MainThread, started
140048551704320)>
Active current thread right now: <Thread(Thread-3, started 140048508823296)>
Value by Thread 3: 3
Active current thread right now: <Thread(Thread-1, started 140048525608704)>
Value by Thread 1: 1
Active current thread right now: <Thread(Thread-2, started 140048517216000)>
Value by Thread 2: 2
2. python 尽量用 threading 模块
但是如果要深入研究就必然去看看底层的_thread api
_thread — Low-level threading API — Python 3.11.3 documentation
threading.Thread 对象:
1. 只能 override __init__和 run 函数
2. run 方法出现了异常,就会调用 threading.expecthook 来 Handle,但是默认情
况下,excepthook 会忽略 SystemExit
3. daemon thread 可能会被突然终止,因为本身考虑是作为“辅助”非守护线程的。
main 线程是初始的控制线程,也就是启动 interpreter 的那个线程
使用 while 循环检查所要求的条件成立与否是有必要的,因为 wait() 方法可能要经过不
确定长度的时间后才会返回,而此时导致 notify() 方法调用的那个条件可能已经不再成立。
这是多线程编程所固有的问题。
t = threading.Thread(target=?,name=?,args=?) -> initialize
有以下方法:
t.start()
t.run()
Commented [F3]: _thread — Low-level
threading API — Python 3.11.3
documentation
Commented [F4]: 守护线程在程序关闭时会
突然关闭。他们的资源(例如已经打开的
文档,数据库事务等等)可能没有被正确
释放。如果你想你的线程正常停止,设置
他们成为非守护模式并且使用合适的信号
机制,例如: Event。
Commented [F5]: Calling the start()
function on a new thread will internally call
the run() function, among other things.
Alternatively, if you defined a custom class
that extended
the threading.Thread class and override
the run() function, then
the start() function would call your
overridden run() function.
剩余12页未读,继续阅读
资源评论
万物琴弦光锥之外
- 粉丝: 346
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 词向量-中文文本相似度计算-采用text2vec词向量工具进行计算对比.zip
- 521293804316625base(1).apk
- ARP IP地址分类及特殊IP
- 【MySQL补丁】vcredist-x64xz MySQL必备插件
- 基于SG3525芯片PWM控制推挽隔离DCDC电源模块AD09设计硬件(原理图+PCB)工程文件.zip
- Word_20240428_092324.docx
- cloudcc_v1.1.10-release_sign.apk
- 基于javaweb(jsp)实现的图书购物网站,基于JSP(MVC模式)和MySQL的网上图书购物系统
- fhdishfifdsf
- javassm源码上传图片功能
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功