multiprocessing - How can I do an atomic write to stdout in python? -


I have read in some sources that print is not a command-thread and secure solution, instead of

Strong> sys.stdout.write command, but still it does not work for me and writing STDOUT is not atom.

is a small example (this file is called parallelExperiment.py):

  Multiprocessing import pool DF output (msg) from import and import import: msg = % S% s '% (msg), Os.linesep) sys.stdout.write (msg) def func (input): Output (UIPID:% d input \ "% s \"'% (os.getpid), Str (input)) has got the DEF Execution Function Inperral (FunkCanM, Input List, Maxoperalism):% d size with the resemblance of% d '% (funcName .__ name__, lane (input list), maxpartarianism) U'Executing function% s) par AllelismPool = Pool = Max PayPalism) Boolean Publish List = Parallelism Map (funcName, inputsList) ParallelismPool.close () output (u'Function% s size% d executed with the maximum likelihood of% d input% d '% (funcName .__ name__, lane (inputsList), maxParallelism) # if all parallel The execution was well executed - Boolean results list All correct return must be all (executeBooleanResultsList) if __name__ == "__main__": inputsList = [for range (i) i in range (20)] executeFunctionInParallel (func, inputsList , 4)  

Look at the output:

i. Python parallelExperiment.py is the production of the call (note that the word "pid" has been messed up in some lines):

  function function on the size of 20 Implementation: Maximum Compatibility of 4 PPID: 2240 Input "0" Id Found: 4960 Input "2" Pin Found: 4716 Input "4" Paid Found: 4324 Input Found "6" PPID: 2240 Input "1" ID Found: 4960 Input "3" PID: 4716 Input "5" PID: 4324 Found Input "7" ppid: 4960 Found Input "8" ID: 2240 Found Input " Found 10 "pid: 4716 found input" 12 ": 4324 input" 14 "got input" 9 "id: 2240 input" 11 "pied found: 4716 input" 13 "pied found: found 4324 input" 15 " Was found on the input of size 20 with the maximum parallelization of "20", "4"   

ii. Calling dragon parallelExperiment.py> parallelExperiment.log means to redirect stdout to the parallelExperiment.log file (note that its command Lines are not good, because executeFunctionInParallel calls in parallel before and after func , a message should be printed):

 multiprocessing.pool  

> actually uses subprocess instead of thread, you are clear between processes Need to use, note the example of the link, it resolves your problem. Import import import from multi-dimensional import pool, lock lock = lock () Diff output (msg): msg = '% s% lock with s'% (msg, os.linesep)

 ): Sys.stdout.write (msg) def func (input): Output (UIPID:% d input \ "% s \" '% (os.getpid), string (input)) DRF execution function InParallel (funcName, InputsList, maxParallelism):% d '% (funcName .__ name__, execute output (u' maximal paralilism) parallelism = pool (processes = max copy) at input% d with maximum parallelism of lane (inputsList) Rambolion results = parallelism (phencalcam, input list) parallelism. Closed () output (u 'function% s% d input% d with the maximum parallelism of% d'% (funcName) with .__ name__, lane ( Input List), Maxoperalism) # If all parallel execution is executed well - Boolean results list should be all correct execute (executeBooleanResultsList) if __name __ == "__main__": inputsList = [range (i) In the category for (20)] execute FunctionInParallel (func, inputsList, 4)  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -