You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
2.7 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### **JIT即时编译**
JITJust-In-Time Compilation即时编译是一种在程序运行时将代码编译为机器码的技术。与传统的 **AOTAhead-Of-Time Compilation提前编译** 不同JIT 在程序执行过程中动态编译代码。JIT 跨平台,生成适合当前平台的机器码。
JIT 的工作原理:**解释执行**:程序开始时,代码以解释方式执行(逐行解释字节码)。 **热点检测**JIT 编译器监控代码执行,识别频繁执行的代码段(称为“热点”)。 **动态编译**:将热点代码编译为机器码,后续执行直接运行机器码,避免解释执行的开销。 **优化**JIT 编译器可以根据运行时信息进行优化(如内联函数、消除死代码)。
在 Python 中利用 JIT 加速的方法包括:
- **PyPy**:通用的 Python 实现适合大多数场景。pypy your_script.py
- **Numba**:专注于数值计算,适合科学计算和数据分析。用 `@jit` 装饰器标记需要加速的函数。
- **Cython**:将 Python 代码编译为 C 代码,适合需要极致性能的场景。支持 JIT 和 AOT 编译 。
- **Taichi**:专注于高性能计算,适合图形学、物理仿真等领域。
### 异步编程生态系统中的几个概念
**异步编程**
异步编程是一种编程范式,允许任务并发执行。
在 Python 中,异步编程可以通过协程、回调、事件循环等多种方式实现。
异步编程适合高并发的 I/O 密集型任务(如 Web 服务器、爬虫、实时通信), 特别是大量并发连接的任务。
**协程**
协程是异步编程的一种实现方式,协程是一种在执行过程中可以暂停和恢复的函数。
协程运行在线程之上,协程的调度完全由用户控制 。
同回调等其他异步技术相比,协程维持了正常的代码流程,提高了代码可读性。
**Async**
Async 是 Python 3.5 引入的一个关键字用于定义异步函数即协程。async def 定义的函数可以暂停执行,使用 await 等待其他操作完成,它们构成了 Python 的异步编程语法。
**asyncio**
asyncio 是 Python 标准库中管理协程的框架。
Python 的协程实现历史
- 生成器协程yield/send
- 原生协程,使用 @asyncio.coroutine 和 yield from ,已废
- 原生协程,自 Python 3.5 起async/await 成为标准 。
- 第三方库 gevent 也有不短的历史 。
### 碎片
- 网络系统常用架构 :服务端用线程池,客户端用 asynico - 异步
- 分布式任务队列系统celery ,不用自己造车 lzuDataFactory
- thread 模块是比较底层的模块, threading 模块对 thread 做了一些包装