子进程执行的顺序及其对操作原子性的影响(order of subprocesses execution and it's impact on operations atomicity)
我正在学习python多处理模块,我发现了这个例子(这是一个有点修改的版本):
#!/bin/env python import multiprocessing as mp import random import string import time # Define an output queue output = mp.Queue() # define a example function def rand_string(length, output): time.sleep(1) """ Generates a random string of numbers, lower- and uppercase chars. """ rand_str = ''.join(random.choice( string.ascii_lowercase + string.ascii_uppercase + string.digits) for i in range(length)) result = (len(rand_str), rand_str) print result time.sleep(1) output.put(result) def queue_size(queue): size = int(queue.qsize()) print size # Setup a list of processes that we want to run processes = [mp.Process(target=rand_string, args=(x, output)) for x in range(1,10)] # Run processes for p in processes: p.start() # Exit the completed processes for p in processes: p.join() # Get process results from the output queue results = [output.get() for p in processes] print(results)
这个输出如下:
(3, 'amF') (1, 'c') (6, '714CUg') (4, '10Qg') (5, 'Yns6h') (7, 'wsSXj3Z') (9, 'KRcDTtVZA') (2, 'Qy') (8, '50LpMzG9') [(3, 'amF'), (1, 'c'), (6, '714CUg'), (4, '10Qg'), (5, 'Yns6h'), (9, 'KRcDTtVZA'), (2, 'Qy'), (7, 'wsSXj3Z'), (8, '50LpMzG9')]
据我所知,进程不会按照它们的创建顺序调用(使用
processes = [mp.Process(target=rand_string, args=(x, output)) for x in range(1,10)]
)这在引用中提到文章。 我不明白(或者我不确定是否理解正确)是为什么result
的顺序与打印输出result
到STDOUT的顺序不一致? 我对此的理解是这三个操作不是原子的(我的意思是它们可以通过进程切换分开):print result time.sleep(1) output.put(result)
基本上,这里发生的是,当进程将
results
results
进程。 像这样的东西:Time ------------------------------------------------------------------------------------------------------------------> Process1: print results | | | time.sleep(1) | output.put(result) | Process2: | print results | time.sleep(1) | output.put(result) | | |
在这种情况下,STDOUT上的输出将是:
(1, 'c') (2, 's5')
但
results
的实际内容将是:
[ (2, 's5') (1, 'c')]
出于同样的原因,这些过程并没有按照它们的创建顺序进行。
我对吗?
I'm learning python multiprocessing module and I've found this example (this is a bit modified version):
#!/bin/env python import multiprocessing as mp import random import string import time # Define an output queue output = mp.Queue() # define a example function def rand_string(length, output): time.sleep(1) """ Generates a random string of numbers, lower- and uppercase chars. """ rand_str = ''.join(random.choice( string.ascii_lowercase + string.ascii_uppercase + string.digits) for i in range(length)) result = (len(rand_str), rand_str) print result time.sleep(1) output.put(result) def queue_size(queue): size = int(queue.qsize()) print size # Setup a list of processes that we want to run processes = [mp.Process(target=rand_string, args=(x, output)) for x in range(1,10)] # Run processes for p in processes: p.start() # Exit the completed processes for p in processes: p.join() # Get process results from the output queue results = [output.get() for p in processes] print(results)
The output of this is following:
(3, 'amF') (1, 'c') (6, '714CUg') (4, '10Qg') (5, 'Yns6h') (7, 'wsSXj3Z') (9, 'KRcDTtVZA') (2, 'Qy') (8, '50LpMzG9') [(3, 'amF'), (1, 'c'), (6, '714CUg'), (4, '10Qg'), (5, 'Yns6h'), (9, 'KRcDTtVZA'), (2, 'Qy'), (7, 'wsSXj3Z'), (8, '50LpMzG9')]
I understand that processes are not called in order which they was created (using
processes = [mp.Process(target=rand_string, args=(x, output)) for x in range(1,10)]
) this is mentioned in referred article. What I do not understand (or I'm not sure if understand correct) is why the order ofresult
does not corresponds with the order in which print outputs theresult
to STDOUT? My understanding of this is that those three operations are not atomic (I mean that they can be separated by process switch):print result time.sleep(1) output.put(result)
Basically what happens here is that in the moment when process
results
to STDOUT it is switched to another process which writes toresults
. Something like that:Time ------------------------------------------------------------------------------------------------------------------> Process1: print results | | | time.sleep(1) | output.put(result) | Process2: | print results | time.sleep(1) | output.put(result) | | |
In this case the output on STDOUT would be:
(1, 'c') (2, 's5')
But the actual content of
results
will be:
[ (2, 's5') (1, 'c')]
And for the same reason the processes are not stared in order as they ware created.
Am I right?
原文:https://stackoverflow.com/questions/32192938
最满意答案
propOrder
基于字段/属性名称而不是XML元素名称。 所以你应该有@XmlType(propOrder = {"name", "active","attributes"})
了解更多信息
The
propOrder
is based on the field/property name and not the XML element name. So you should have@XmlType(propOrder = {"name", "active","attributes"})
For More Information
相关问答
更多-
propOrder基于字段/属性名称而不是XML元素名称。 所以你应该有 @XmlType(propOrder = {"name", "active","attributes"}) 了解更多信息 http://blog.bdoughan.com/2012/02/jaxbs-xmltype-and-proporder.html The propOrder is based on the field/property name and not the XML element name. So you shou ...
-
将listProperties初始化为链接列表: @XmlElement(name = "list-property") public List
listProperties = new LinkedList (); 然后unmarshaller将它作为链表返回。 Initialize your listProperties as a linked list : @XmlElement(name = "list-property") public Li ... -
简单的答案是肯定的。 列表是按顺序迭代的,并且filter生成器按照该顺序逐个读取每个项目。 因此,它将按顺序产出产量。 >>> example = list(range(10)) >>> list(filter(lambda n: n % 2, example)) [1, 3, 5, 7, 9] The simple answer is yes. Lists are ordered iterables, and the filter generator reads each item in, one a ...
-
按id示例排序对象: var employees=[] employees[0]={id: 1, name:"George", age:32} employees[1]={id: 4, name:"Edward", age:17} employees[2]={id: 2, name:"Christine", age:58} employees[3]={id: 3, name:"Sarah", age:62} var sorte ...
-
经过数小时的调查,我发现了这个问题。 这是由于hibernate将仍然延迟加载的对象传递给MarshallingPayloadMethodProcessor。 我只是假设在编组之前对象将被正确初始化。 我创建了一个自定义的XmlAdapter: public class HibernatetEntityAdapter extends XmlAdapter
{ @Override public AbstractEntity unmarshal( ... -
如何编写我的模式,以便按顺序将元素映射到单独的属性而不是JAXB生成的类中的一个List [2021-04-29]
首先是:你真的需要mixed="true"吗? 这意味着在FooType的所有子元素之间,您也可以拥有内容。 这意味着您需要存储String的混合物(用于子元素之前,之间和之后的内容),一个Bar,一个Baz以及xs:any 。 真是一种混合物。 没有混合,xjc会生成: public class FooType { @XmlElement(name = "Bar") protected BigInteger bar; @XmlElement(name = "Baz") pr ... -
如果您将maxOccurs设置为!= 1,那么它可以包含该元素的多个实例,因此它将成为一个列表。 您应该使用get方法,然后将元素添加到该列表。 像这样的东西: List
myList = doc.getNameOfElement(); myList.add(obj); 编辑:如果你已经有一个你想使用的列表,你可以做以下的事情: doc.getNameOfElement().addAll(myList); If you have maxOccurs set t ... -
如何使用JAXB编组带有XML根元素的Java List?(How to marshal a java List with out XML root element using JAXB?)[2022-05-22]
您可以遍历列表,单独编组每个项目。 您需要在Marshaller上设置JAXB_FRAGMENT属性,以防止写出XML标头。 对于此用例,您只需要创建一次JAXBContext和Marshaller 。 import java.io.FileOutputStream; import java.util.*; import javax.xml.bind.*; public class Demo { public static void main(String[] args) throws Exce ... -
用于对列表元素进行排序和分组的算法或数据结构(Algorithm or data structure for ordering and grouping list elements)[2021-12-19]
您使用下拉菜单并且存在用户交互,因此我支持您的应用程序是一种web或gui应用程序。 如果使用数据库,则可以将树/图表选项和子选项存储在一个表或两个表中。 如果你不能使用数据库,你可以创建一个图形数据结构和相关的算法来操作它,具体取决于你的图形实现数组,指针.. 在Java编程语言中: class Option { ... List -
是否可以使用JAXB基于某些属性值获取XML元素?(Is it possible to get XML elements based on some attribute value/s using JAXB?)[2022-08-27]
你有一个List heightList,它可以从文档/对象树中检索而不需要任何额外的开销 - 它是该树的一部分。 部分解组没有完成。 要使用特定ID值找到高度: Height getById( Listlist, Integer id ){ for( Height h: list ){ if( h.getId().equals(id) ) return h; } return null; } 可能值得考虑像XSLT这样的东西来将XML文档减少到包含 ...