我想将一些数据输出到管道,并让另一个进程逐行对数据执行某些操作.这是一个玩具示例:
mkfifo pipe
cat pipe&
cat >pipe
Run Code Online (Sandbox Code Playgroud)
现在我可以输入我想要的任何内容,按下回车后我立即看到同一行.但如果替换第二管道echo:
mkfifo pipe
cat pipe&
echo "some data" >pipe
Run Code Online (Sandbox Code Playgroud)
管道在完成之后关闭echo,cat pipe&因此我无法通过管道传递更多数据.有没有办法避免关闭管道和接收数据的进程,以便我可以从bash脚本通过管道传递多行数据,并在它们到达时处理它们?
阅读咸菜文档后,我得到了一个类需要实施的印象__reduce__或__getstate__得到正确腌制.但是,字典的酸洗工作呢?他们没有任何这些属性:
> dict(a=1).__reduce__()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/daniyar/work/Apr24/<ipython-input-30-bc1cbd43305b> in <module>()
----> 1 dict(a=1).__reduce__()
/usr/lib/python2.6/copy_reg.pyc in _reduce_ex(self, proto)
68 else:
69 if base is self.__class__:
---> 70 raise TypeError, "can't pickle %s objects" % base.__name__
71 state = base(self)
72 args = (self.__class__, base, state)
TypeError: can't pickle dict objects
> dict(a=1).__getstate__()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/home/daniyar/work/Apr24/<ipython-input-31-00932fb40067> in <module>()
----> 1 dict(a=1).__getstate__()
AttributeError: 'dict' object has no attribute '__getstate__'
Run Code Online (Sandbox Code Playgroud)
另外,从dict派生的类是如何被腌制的?
我有一些完美的python代码,它使用了多处理模块,并在我的机器上以100%加载了所有8个CPU.
在我从Ubuntu 10.10升级到12.04之后(最明显的事情,也许我做了别的事情打破了一切),它停止了工作.经过大量调试后,我发现即使在最简单的用例中,两个模块也仅使用1个CPU:
from pylab import *
import multiprocessing as mp
from joblib import Parallel, delayed
def f(i):
# Slow calculation
x = 1
for j in range(100000): x = cos(x)
print i, x
if __name__ == '__main__':
# Try to multiprocess with multiprocessing
mp.Pool(processes=8).map(f, range(100))
# Try to multiprocess with joblib
Parallel(n_jobs=8)(delayed(f)(i) for i in range(100))
Run Code Online (Sandbox Code Playgroud)
我需要在我的系统中使用所有8个CPU.我应该注意什么想法来解决这个问题?
编辑:
正如ali_m在这里的评论和回答中指出的那样
为什么多重处理在我导入numpy之后只使用一个核心?问题与numpy搞乱cpu亲和力有关.调用
os.system('taskset -p 0xffffffff %d' % os.getpid())
Run Code Online (Sandbox Code Playgroud)
在我做任何多处理之前解决了我的问题.
我想在Python中使用+ =表示法来更新类似dict的对象.我想要与dict.update方法具有相同的行为.这是我的班级(带有"."访问权限的字典):
class sdict(dict):
def __getattr__(self, attr):
return self.get(attr, None)
__setattr__= dict.__setitem__
__delattr__= dict.__delitem__
Run Code Online (Sandbox Code Playgroud)
我试过了:
__iadd__ = dict.update
Run Code Online (Sandbox Code Playgroud)
和:
def __iadd__(self, other):
self.update(other)
return self
Run Code Online (Sandbox Code Playgroud)
但这些都不起作用.(第一个破坏原始字典,第二个生成SyntaxError)
更新:
第二个定义实际上有效.它对我不起作用,因为我忘记了def.第一个不起作用,因为dict.update返回None.
这是我需要挑选的课程:
class sdict(dict):
def __getattr__(self, attr):
return self.get(attr, None)
__setattr__= dict.__setitem__
__delattr__= dict.__delitem__
__reduce__ = dict.__reduce__
Run Code Online (Sandbox Code Playgroud)
在我看来__reduce__应该照顾酸洗,但相反:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/daniyar/work/Apr24/<ipython-input-28-1cc94b920737> in <module>()
----> 1 pickle.dumps(sdict(a=1))
/usr/lib/python2.6/pickle.pyc in dumps(obj, protocol)
1364 def dumps(obj, protocol=None):
1365 file = StringIO()
-> 1366 Pickler(file, protocol).dump(obj)
1367 return file.getvalue()
1368
/usr/lib/python2.6/pickle.pyc in dump(self, obj)
222 if self.proto >= 2:
223 self.write(PROTO + chr(self.proto))
--> 224 self.save(obj)
225 self.write(STOP)
226
/usr/lib/python2.6/pickle.pyc in save(self, obj)
304 reduce = getattr(obj, "__reduce_ex__", …Run Code Online (Sandbox Code Playgroud)