[PYTHON] Difference in results depending on the argument of multiprocess.Process

Test.py


from multiprocessing import Process
from random import random
class Test:
    def __init__(self):
        self.val= 0.0

    def func(self):
        self.val=random()
        print self.val

if __name__ == '__main__':

    tests=[]
    prcs= []
    for i in range(2):
        tests.append(Test())
        prcs.append( Process(target=tests[i].func) )
    for p in prcs:
        p.start()
    for p in prcs:
        p.join()
    for t in tests:
        print t.val

When you run such a program,

$ python Test.py
0.577127051322
0.331891187398
0.0
0.0

The result is. In other words, the value stored in self.val inside the functionfunc ()is invalid after that. On the other hand, add () to the function passed to Process, that is,

        prcs.append( Process(target=tests[i].func()) )

If you change it like

$ python Test.py
0.872880584458
0.255770370005
0.872880584458
0.255770370005

The self.val value assigned in the function func is stored firmly as shown in.

I don't understand what this means. .. ..

Recommended Posts

Difference in results depending on the argument of multiprocess.Process
[Linux] Difference in time information depending on the clock ID of the clock_gettime () function
In Python, change the behavior of the method depending on how it is called
rsync Behavior changes depending on the presence or absence of the slash in the copy source
Find the difference in Python
Reuse the results of clustering
A note on the default behavior of collate_fn in PyTorch
Survey on the use of machine learning in real services
Analyzing data on the number of corona patients in Japan
[Jinja2] Changes in line breaks depending on the hyphen position
Count the number of characters in the text on the clipboard on mac
Data analysis based on the election results of the Tokyo Governor's election (2020)
Verification of the spread of hoaxes in the "State of Emergency Declaration on April 1"
The story of participating in AtCoder
Django: Fluctuate the number of child forms depending on the number of input items
The story of the "hole" in the file
Difference in execution speed depending on how to write Cython function
Find the rank of a matrix in the XOR world (rank of a matrix on F2)
Yield in a class that inherits unittest.TestCase didn't work with nose (depending on the version of nose?)
The meaning of ".object" in Django
Illustration of the results of the knapsack problem
Get the number of readers of a treatise on Mendeley in Python
[Python] Explore the characteristics of the titles of the top sites in Google search results
Let's change the color scheme of iTerm2 automatically depending on the time zone
Play the comment of Nico Nico Douga on the terminal in conjunction with the video
[Android] Display images on the web in the info Window of Google Map
Receive a list of the results of parallel processing in Python with starmap
You can see the transition of points in the J League on the graph!
The process of repeatedly extracting an array differs slightly depending on the language ...
Check the behavior of destructor in Python
The story of an error in PyOCR
Investigate the effect of outliers on correlation
Implement part of the process in C ++
Post the subject of Gmail on twitter
The result of installing python in Anaconda
Let's claim the possibility of pyenv-virtualenv in 2021
Display the graph of tensorBoard on jupyter
The basics of running NoxPlayer in Python
Difference in output of even-length window function
Change the order of PostgreSQL on Heroku
In search of the fastest FizzBuzz in Python
Make the default value of the argument immutable
How the reference of the python array changes depending on the presence or absence of subscripts
I want to use Python in the environment of pyenv + pipenv on Windows 10
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
Let's automatically display the lyrics of the song being played on iTunes in Python
Set an upper limit on the number of recursive function iterations in Python
Visualize the timeline of the number of issues on GitHub assigned to you in Python
Output the number of CPU cores in Python
The meaning of {version-number} in the mysql rpm package
[Python] Sort the list of pathlib.Path in natural sort
Run the task in the background on the sshed server
Change the font size of the legend in df.plot
Get the caller of a function in Python
Match the distribution of each group in Python
View the result of geometry processing in Python
The behavior of signal () depends on the compile options
[2020July] Check the UDID of the iPad on Linux
Make a copy of the list in Python
Use the latest version of PyCharm on Ubuntu
About the difference between "==" and "is" in python