codecamp

Python 线程安全规范 | Google 官方并发指南

线程

Tip
不要依赖内置类型的原子性。

虽然 Python 的内置类型表面上有原子性,但是在特定情形下可能打破原子性(例如用Python实现 __hash__ 或 __eq__ 的情况下)。因此它们的原子性不可靠。你也不能臆测赋值是原子性的(因为赋值的原子性依赖于字典的原子性)。

选择线程间的数据传递方式时,应优先考虑 queue 模块的 Queue 数据类型。如果不适用,则使用 threading 模块及其提供的锁原语(locking primitives)。如果可行,应该用条件变量和 threading.Condition 替代低级的锁。

Python 装饰器使用规范 | Google 官方指南
Python 禁用的黑魔法 | Google 官方避坑指南
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }