main.py
import moduleA
import matplotlib
if __name__ == "main":
# do something with both library
moduleA.py
import matplotlib
if __name__ == "main":
# do something with matplotlib
1.上面的情况会造成matplotlib包导入两次,花费两次的时间吗?
2.我记得每个python interpreter只会导入一次同一个包,如果这个成立,那如果main使用多进程调用moduleA的话呢?
1
seki 2015-04-21 10:26:21 +08:00
|
2
VYSE 2015-04-21 10:44:46 +08:00 via Android
一次,所以才能monkey patch
|
3
washinriver 2015-04-21 10:46:45 +08:00
不管在哪里导入,只会存在一份包, 在sys.modules下可以看到.
|
5
VYSE 2015-04-21 17:37:01 +08:00 1
|
6
washinriver 2015-04-21 17:50:57 +08:00
@julyclyde gevent实现了一个socket, patch的结果就是拿自己的socket替代了python builtin的socket
|
7
messense 2015-04-21 18:06:31 +08:00 1
@VYSE 你说的有问题........
In [1]: import sys In [2]: import gevent.socket as socket In [3]: socket Out[3]: <module 'gevent.socket' from '/home/hongyu/refactor/lib/python2.6/site-packages/gevent/socket.pyc'> In [4]: import socket In [5]: socket Out[5]: <module 'socket' from '/usr/lib64/python2.6/socket.pyc'> monkey patch 不是这样的,而是类似: import urllib def my_urlencode(query, doseq=0): ----pass urllib.urlencode = my_urlencode In[6]: urllib.urlencode Out[6]: <function __main__.my_urlencode> |