multiprocessing 重复执行代码
if __name__ == '__main__':
解决:只能在程序入口内调用multiprocessing
- __main__ :The name of the top-level environment of the program, which can be checked using the
__name__ == '__main__'expression
Is the name of the environment where top-level code is run. “Top-level code” is the first user-specified Python module that starts running. It' s "top-level" because it imports all other modules that the program needs. Sometimes “top-level code” is called an entry point to the application.
A module can discover whether or not it is running in the top-level environment by checking its own __name__, which allows a common idiom for conditionally executing code when the module is not initialized from an import statement: if __name__ == '__main__':
Ubuntu安装指定版本Python
PPA安装
步骤
更新系统软件包
sudo apt updatesudo apt upgrade
导入PPA
sudo add-apt-repository ppa:deadsnakes/ppa
再次更新apt本地软件包版本缓存
sudo apt update
安装指定版本Python
sudo apt install python[version]
设置pip给新安装的Python安装包
sudo apt install python[version]-distutils- 临时
python[version] -m pip install xxx
- 永久
- 在/bin目录下修改默认Python的软连接:
ln -snf python[version] python
- 在/bin目录下修改默认Python的软连接:
可能遇到的问题
报错
update-initramfs: failed for /boot/initrd.img-6.5.0-44-generic with 1.
dpkg: error processing package initramfs-tools (--configure):
installed initramfs-tools package post-installation script subprocess returned error exit status 1.
原因
- /boot区空间不足,initramfs无法生成
解决
- 删除/boot下多余的旧内核,释放空间
sudo apt purge/autoremove linux-image-xxx- 若空间太小无法删除,直接rm /boot目录下文件
- 删除后可以安装最新内核,之后再将旧内核全部删掉
相对路径引入包
报错
from .edge_sim_py import *
ImportError: attempted relative import with no known parent package
原因
-
模块:包含Python定义和语句的文件:.py,模块内可以通过import语句发起调用导入来访问另一个模块的内容,import语句执行:模块搜索 + 名称绑定
- 搜索路径:
sys.builtin_module_namessys.path:被命令直接运行的脚本所在的目录;PYTHONPATH;site-packages目录
- 搜索路径:
-
包:包含模块或子包的抽象,任何具有__path__属性的模块都是包
- 常规包:一个包含__init__.py文件的目录,被导入时__init__.py隐式执行
- 命名空间包:
- 导入:
- 绝对导入:
from x.y import z - 相对导入:
from .x import y
- 绝对导入:
-
报错原因:相对导入只能在包内,而脚本所在目录是顶层目录
解决
- 使用绝对路径:
from edge_sim_py import *
类的特殊方法
使用方式:加装饰器
特殊方法:
- 实例方法:默认,不使用装饰器
class C:
def instance_method(self):
pass
- 类方法:使用
@classmethod装饰器,可访问类属性和方法
class C:
@classmethod
def class_method(cls):
pass
- 静态方法:使用
@staticmethod装饰器,类内独立函数
class C:
@staticmethod
def static_method():
pass
多线程、多进程
多线程 threading
CPython
- CPython是由C语言开发的官方Python解释器,它将Python代码编译成字节码(__pycache__/xxx.pyc),再通过Python虚拟机(一个专门解析并执行字节码的C程序)执行字节码
全局解释器锁(GIL)
- 在CPython中,GIL确保同一时刻只有一个线程在执行Python字节码,牺牲了并行性
- GIL不保证线程安全,因为字节码粒度过细,一个语句会被截断执行,如在访问全局变量时会产生问题
处理线程安全问题
- threading.Lock:互斥锁,可以被获取(acquire)或释放(release)
- threading.RLock:重入锁,可被同一线程多次获取条件变量
- threading.Condition:条件变量,通过wait释放锁并阻塞、notify或notify_all唤醒
- threading.semaphore:信号量,管理一个计数器,acquire减一、release加一
多进程 multiprocessing
安装Anaconda后不显示(base)
conda
set-executionpolicy remotesigned
安装torch-geometric
- 从 https://data.pyg.org/whl/ 中安装相应版本 torch_cluster、torch_scatter、torch_sparse
- pip install torch-geometric
Python 代码执行顺序
- 顶层代码:不在任何函数、类或控制语句内的代码
- 顶层代码在模块(文件)被import时从上到下立即执行,且只执行一次
- 顶层代码中定义的对象会被缓存到
sys.modules中,持续存在,再次引入时只读取缓存
Python 特殊值
nan:float('nan'),不与任何值(包括自身)相等,需通过 np.isnan() 或 pd.isna() 判断
VSCode 悬浮提示 + 转到定义
安装 Pylance 插件
VSCode 同步bug
如果当前路径所在文件夹在另一个连接中被移动或重命名,本终端的路径仍然不变
解决:退出重进文件夹
Python的None可以作为字典的键
VSCode
- 调试配置文件 launch.json 中,可通过
cwd参数修改工作目录
评论区