Home
Sign Up
Sign In
推荐学习书目
›
Learn Python the Hard Way
Python Sites
›
PyPI
- Python Package Index
›
http://diveintopython.org/toc/index.html
›
Pocoo
值得关注的项目
›
PyPy
›
Celery
›
Jinja2
›
Read the Docs
›
gevent
›
pyenv
›
virtualenv
›
Stackless Python
›
Beautiful Soup
›
结巴中文分词
›
Green Unicorn
›
Sentry
›
Shovel
›
Pyflakes
›
pytest
Python 编程
›
pep8 Checker
Styles
›
PEP 8
›
Google Python Style Guide
›
Code Style from The Hitchhiker's Guide
V2EX
›
Python
小白请教各位大神一个关于单例模式的问题
jotsai
·
Dec 21, 2016
· 2069 views
This topic created in 3496 days ago, the information mentioned may be changed or developed.
有个类,在子进程里到处被 import ,要求子进程里创建的实例始终为同一个,但不同的子进程里的实例不一样,请教各位大神如何解决?
实例
进程
请教
单例
5 replies
•
2016-12-23 13:02:30 +08:00
1
sleshep
Dec 21, 2016
都不同进程了,除非写__getstate__方法用来 pickle ,否则没法搞
2
knightdf
Dec 21, 2016
这已经不是单例模式做的事了,参考进程共享数据把
简单可以用外置数据,非要 python 单例对象的话,可以试试代理对象
3
jotsai
OP
Dec 21, 2016
@
sleshep
@
knightdf
可能我表述没到位,我不需要跨进程的单例,我需要的是跨模块的单例
我照着 stackoverflow 的例子实现了一个装饰器,但是每次在别的模块里 import 之后,装饰器用来保存实例的字典就重置了……
开始怀疑自己的思路是不是跑偏了=。=
4
enenaaa
Dec 22, 2016
代码贴出来呗
5
jotsai
OP
Dec 23, 2016
@
enenaaa
@
knightdf
@
sleshep
用 pickle 测试了一下,目前来看是可行的,不过我遇到一个问题,当我用装饰器包装类的时候,这个类就不能被 pickle 序列化了,会报 can ‘ t pickle class x.y it's not the same object as x.y
About
·
Help
·
Advertise
·
Blog
·
API
·
FAQ
·
Solana
·
1051 Online
Highest 6679
·
Select Language
创意工作者们的社区
World is powered by solitude
VERSION: ce1cc7fc · 33ms ·
UTC 18:44
·
PVG 02:44
·
LAX 11:44
·
JFK 14:44
♥ Do have faith in what you're doing.
❯