English 中文(简体)
加入 粉碎
原标题:Joining concurrent Python Output

我这样说:

find folder/ | xargs -n1 -P10 ./logger.py > collab

在<代码>logger.py上 我正在处理产出调整项目的文件。 因此,串通一气。

{ filename  :  file1 ,  size  : 1000}
{ filename  :  file1 ,  size  : 1000}
{ filename  :  file1 ,  size  : 1000}
{ filename  :  file1 ,  size  : 1000}

有时这条线被 j倒:

{ filename  :  file1 ,  size  : 1000}
{ file
{ filename  :  file1 ,  size  : 1000}
name  :  file1 ,  size  : 1000}
{ filename  :  file1 ,  size  : 1000}

我如何能够防止/纠正这种情况?

问题回答

总的来说,有一些问题使得很难做到guarantee,而没有陷入多过程。 然而,你通常可以大量减少这一问题。

最常见的原因是I/O缓冲,在沙里或平衡。 例如,它可能缓冲16k的产出,然后一度书写整块。 你们可以通过在写完字后抽出 st来减少这种情况,但这种.。 从理论上讲,你应当能够通过<条码>-u> ,以达到令人难以置信的缓冲,但当我尝试时,那就没有工作了。 见Sebastjan对,用于更通用的解决办法(尽管可能有一种办法可以更直接地缓冲可变的产出)。

A second problem is that the underlying writes aren t always atomic. In particular, writes to pipes are only atomic up to a certain size (PIPE_BUF, usually 512 bytes); above that it s not guaranteed. That only strictly applies to pipes (not files), but the same general issues apply: smaller writes are more likely to happen atomically. See http://www.opengroup.org/onlinepubs/000095399/functions/write.html.

复杂的、技术上正确的解决办法是执行一个供撰写的双向办法,但我认为,这种分性办法并非理想。

而且,它并非一帆风顺。 如何从Xargs中解放出产出(这样,你就能够掌握产出的坚固丘数,而不是产出的流,然后将这些草块混为一谈?

问题是,来自xargs的产出是混合的。 GNU 解决该问题也是同时进行的。 违约后,保证产出不一而足。 因此,你只能这样做:

find folder/ | parallel ./logger.py > collab

This will run one logger.py per CPU. If you want 10:

find folder/ | parallel -P10 ./logger.py > collab

http://www.you.com/watch?v=OpaiGYxkSuQ” rel=“nofollow” http://www.you.com/watch?v=OpaiGYxkSuQ





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签