Using globally declared tuple for Python multiprocessing












0















I am having some trouble using a tuple globally with the multiprocessing class.



I have a code as produced below:



from multiprocessing import Pool

if __name__ == '__main__':
jj = ()
def f(x):
global jj
jj += (x*x,)

# Section A
#for ii in range(20):
# f(ii)
#print (jj)

# Section B
pool = Pool(processes=4)
pool.map(f, range(20))
pool.join()
print (jj)


If I run section only B, I get the tuple jj as an empty tuple. However, if I run only section A, I get a tuple of length 20.



Why is that so?










share|improve this question

























  • Please run either section A or section B at a time. Comment out the other section not in use. I use tuple because in my original code, I have to store a lot of variables and I read that the instantiation of tuples is much faster than a list. I am not calling these values anywhere in the code.

    – Shihab Khan
    Nov 22 '18 at 8:59
















0















I am having some trouble using a tuple globally with the multiprocessing class.



I have a code as produced below:



from multiprocessing import Pool

if __name__ == '__main__':
jj = ()
def f(x):
global jj
jj += (x*x,)

# Section A
#for ii in range(20):
# f(ii)
#print (jj)

# Section B
pool = Pool(processes=4)
pool.map(f, range(20))
pool.join()
print (jj)


If I run section only B, I get the tuple jj as an empty tuple. However, if I run only section A, I get a tuple of length 20.



Why is that so?










share|improve this question

























  • Please run either section A or section B at a time. Comment out the other section not in use. I use tuple because in my original code, I have to store a lot of variables and I read that the instantiation of tuples is much faster than a list. I am not calling these values anywhere in the code.

    – Shihab Khan
    Nov 22 '18 at 8:59














0












0








0








I am having some trouble using a tuple globally with the multiprocessing class.



I have a code as produced below:



from multiprocessing import Pool

if __name__ == '__main__':
jj = ()
def f(x):
global jj
jj += (x*x,)

# Section A
#for ii in range(20):
# f(ii)
#print (jj)

# Section B
pool = Pool(processes=4)
pool.map(f, range(20))
pool.join()
print (jj)


If I run section only B, I get the tuple jj as an empty tuple. However, if I run only section A, I get a tuple of length 20.



Why is that so?










share|improve this question
















I am having some trouble using a tuple globally with the multiprocessing class.



I have a code as produced below:



from multiprocessing import Pool

if __name__ == '__main__':
jj = ()
def f(x):
global jj
jj += (x*x,)

# Section A
#for ii in range(20):
# f(ii)
#print (jj)

# Section B
pool = Pool(processes=4)
pool.map(f, range(20))
pool.join()
print (jj)


If I run section only B, I get the tuple jj as an empty tuple. However, if I run only section A, I get a tuple of length 20.



Why is that so?







python python-3.x python-multiprocessing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 9:00







Shihab Khan

















asked Nov 22 '18 at 8:45









Shihab KhanShihab Khan

227




227













  • Please run either section A or section B at a time. Comment out the other section not in use. I use tuple because in my original code, I have to store a lot of variables and I read that the instantiation of tuples is much faster than a list. I am not calling these values anywhere in the code.

    – Shihab Khan
    Nov 22 '18 at 8:59



















  • Please run either section A or section B at a time. Comment out the other section not in use. I use tuple because in my original code, I have to store a lot of variables and I read that the instantiation of tuples is much faster than a list. I am not calling these values anywhere in the code.

    – Shihab Khan
    Nov 22 '18 at 8:59

















Please run either section A or section B at a time. Comment out the other section not in use. I use tuple because in my original code, I have to store a lot of variables and I read that the instantiation of tuples is much faster than a list. I am not calling these values anywhere in the code.

– Shihab Khan
Nov 22 '18 at 8:59





Please run either section A or section B at a time. Comment out the other section not in use. I use tuple because in my original code, I have to store a lot of variables and I read that the instantiation of tuples is much faster than a list. I am not calling these values anywhere in the code.

– Shihab Khan
Nov 22 '18 at 8:59












1 Answer
1






active

oldest

votes


















1














Ok, as Python multiprocessing global variable updates not returned to parent explains, global state is not shared among processes.



You can share state using, for example, multiprocessing.Queue.



from multiprocessing import Pool, Queue

if __name__ == "__main__":
jj = ()
q = Queue()

def f(x):
global jj
jj += (x * x,)

def f_multi(x):
q.put(x * x)

# Section A
for ii in range(20):
f(ii)
print(jj)

# Section B
pool = Pool(processes=4)
pool.map(f_multi, range(20))
pool.close()

stop = "STOP"
q.put(stop)
items =
for i in iter(q.get, stop):
items.append(i)

print(tuple(items))


Alternatively you can use print(tuple(sorted(items))) to get the values in the same order as Section A will produce. 4 processes are working on the task in Section B and hence the "unordered" result.






share|improve this answer


























  • Thanks a lot. Actually @Dušan, the problem which I am facing is that I don't have a single variable I'd have to put in a Queue. I have a function that generates a lot of data. I'm trying to store that data from each iteration so that I can use them later on for validating/checking my results. Is there any efficient way to do that? It seems to be that I'll have to do this for each of the variables.

    – Shihab Khan
    Nov 22 '18 at 9:34






  • 1





    You can put all variables you want to return from your function to a dict and then put that dict to the Queue, e.g. q.put({'data1': [1,2,3], 'data2': (0.5, 0.6)}).

    – Dušan Maďar
    Nov 22 '18 at 9:43













  • This sounds like a good idea. If I can make the function return this dictionary then I don't even have to use a queue.

    – Shihab Khan
    Nov 22 '18 at 9:53











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426937%2fusing-globally-declared-tuple-for-python-multiprocessing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Ok, as Python multiprocessing global variable updates not returned to parent explains, global state is not shared among processes.



You can share state using, for example, multiprocessing.Queue.



from multiprocessing import Pool, Queue

if __name__ == "__main__":
jj = ()
q = Queue()

def f(x):
global jj
jj += (x * x,)

def f_multi(x):
q.put(x * x)

# Section A
for ii in range(20):
f(ii)
print(jj)

# Section B
pool = Pool(processes=4)
pool.map(f_multi, range(20))
pool.close()

stop = "STOP"
q.put(stop)
items =
for i in iter(q.get, stop):
items.append(i)

print(tuple(items))


Alternatively you can use print(tuple(sorted(items))) to get the values in the same order as Section A will produce. 4 processes are working on the task in Section B and hence the "unordered" result.






share|improve this answer


























  • Thanks a lot. Actually @Dušan, the problem which I am facing is that I don't have a single variable I'd have to put in a Queue. I have a function that generates a lot of data. I'm trying to store that data from each iteration so that I can use them later on for validating/checking my results. Is there any efficient way to do that? It seems to be that I'll have to do this for each of the variables.

    – Shihab Khan
    Nov 22 '18 at 9:34






  • 1





    You can put all variables you want to return from your function to a dict and then put that dict to the Queue, e.g. q.put({'data1': [1,2,3], 'data2': (0.5, 0.6)}).

    – Dušan Maďar
    Nov 22 '18 at 9:43













  • This sounds like a good idea. If I can make the function return this dictionary then I don't even have to use a queue.

    – Shihab Khan
    Nov 22 '18 at 9:53
















1














Ok, as Python multiprocessing global variable updates not returned to parent explains, global state is not shared among processes.



You can share state using, for example, multiprocessing.Queue.



from multiprocessing import Pool, Queue

if __name__ == "__main__":
jj = ()
q = Queue()

def f(x):
global jj
jj += (x * x,)

def f_multi(x):
q.put(x * x)

# Section A
for ii in range(20):
f(ii)
print(jj)

# Section B
pool = Pool(processes=4)
pool.map(f_multi, range(20))
pool.close()

stop = "STOP"
q.put(stop)
items =
for i in iter(q.get, stop):
items.append(i)

print(tuple(items))


Alternatively you can use print(tuple(sorted(items))) to get the values in the same order as Section A will produce. 4 processes are working on the task in Section B and hence the "unordered" result.






share|improve this answer


























  • Thanks a lot. Actually @Dušan, the problem which I am facing is that I don't have a single variable I'd have to put in a Queue. I have a function that generates a lot of data. I'm trying to store that data from each iteration so that I can use them later on for validating/checking my results. Is there any efficient way to do that? It seems to be that I'll have to do this for each of the variables.

    – Shihab Khan
    Nov 22 '18 at 9:34






  • 1





    You can put all variables you want to return from your function to a dict and then put that dict to the Queue, e.g. q.put({'data1': [1,2,3], 'data2': (0.5, 0.6)}).

    – Dušan Maďar
    Nov 22 '18 at 9:43













  • This sounds like a good idea. If I can make the function return this dictionary then I don't even have to use a queue.

    – Shihab Khan
    Nov 22 '18 at 9:53














1












1








1







Ok, as Python multiprocessing global variable updates not returned to parent explains, global state is not shared among processes.



You can share state using, for example, multiprocessing.Queue.



from multiprocessing import Pool, Queue

if __name__ == "__main__":
jj = ()
q = Queue()

def f(x):
global jj
jj += (x * x,)

def f_multi(x):
q.put(x * x)

# Section A
for ii in range(20):
f(ii)
print(jj)

# Section B
pool = Pool(processes=4)
pool.map(f_multi, range(20))
pool.close()

stop = "STOP"
q.put(stop)
items =
for i in iter(q.get, stop):
items.append(i)

print(tuple(items))


Alternatively you can use print(tuple(sorted(items))) to get the values in the same order as Section A will produce. 4 processes are working on the task in Section B and hence the "unordered" result.






share|improve this answer















Ok, as Python multiprocessing global variable updates not returned to parent explains, global state is not shared among processes.



You can share state using, for example, multiprocessing.Queue.



from multiprocessing import Pool, Queue

if __name__ == "__main__":
jj = ()
q = Queue()

def f(x):
global jj
jj += (x * x,)

def f_multi(x):
q.put(x * x)

# Section A
for ii in range(20):
f(ii)
print(jj)

# Section B
pool = Pool(processes=4)
pool.map(f_multi, range(20))
pool.close()

stop = "STOP"
q.put(stop)
items =
for i in iter(q.get, stop):
items.append(i)

print(tuple(items))


Alternatively you can use print(tuple(sorted(items))) to get the values in the same order as Section A will produce. 4 processes are working on the task in Section B and hence the "unordered" result.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 9:32

























answered Nov 22 '18 at 9:26









Dušan MaďarDušan Maďar

4,37742035




4,37742035













  • Thanks a lot. Actually @Dušan, the problem which I am facing is that I don't have a single variable I'd have to put in a Queue. I have a function that generates a lot of data. I'm trying to store that data from each iteration so that I can use them later on for validating/checking my results. Is there any efficient way to do that? It seems to be that I'll have to do this for each of the variables.

    – Shihab Khan
    Nov 22 '18 at 9:34






  • 1





    You can put all variables you want to return from your function to a dict and then put that dict to the Queue, e.g. q.put({'data1': [1,2,3], 'data2': (0.5, 0.6)}).

    – Dušan Maďar
    Nov 22 '18 at 9:43













  • This sounds like a good idea. If I can make the function return this dictionary then I don't even have to use a queue.

    – Shihab Khan
    Nov 22 '18 at 9:53



















  • Thanks a lot. Actually @Dušan, the problem which I am facing is that I don't have a single variable I'd have to put in a Queue. I have a function that generates a lot of data. I'm trying to store that data from each iteration so that I can use them later on for validating/checking my results. Is there any efficient way to do that? It seems to be that I'll have to do this for each of the variables.

    – Shihab Khan
    Nov 22 '18 at 9:34






  • 1





    You can put all variables you want to return from your function to a dict and then put that dict to the Queue, e.g. q.put({'data1': [1,2,3], 'data2': (0.5, 0.6)}).

    – Dušan Maďar
    Nov 22 '18 at 9:43













  • This sounds like a good idea. If I can make the function return this dictionary then I don't even have to use a queue.

    – Shihab Khan
    Nov 22 '18 at 9:53

















Thanks a lot. Actually @Dušan, the problem which I am facing is that I don't have a single variable I'd have to put in a Queue. I have a function that generates a lot of data. I'm trying to store that data from each iteration so that I can use them later on for validating/checking my results. Is there any efficient way to do that? It seems to be that I'll have to do this for each of the variables.

– Shihab Khan
Nov 22 '18 at 9:34





Thanks a lot. Actually @Dušan, the problem which I am facing is that I don't have a single variable I'd have to put in a Queue. I have a function that generates a lot of data. I'm trying to store that data from each iteration so that I can use them later on for validating/checking my results. Is there any efficient way to do that? It seems to be that I'll have to do this for each of the variables.

– Shihab Khan
Nov 22 '18 at 9:34




1




1





You can put all variables you want to return from your function to a dict and then put that dict to the Queue, e.g. q.put({'data1': [1,2,3], 'data2': (0.5, 0.6)}).

– Dušan Maďar
Nov 22 '18 at 9:43







You can put all variables you want to return from your function to a dict and then put that dict to the Queue, e.g. q.put({'data1': [1,2,3], 'data2': (0.5, 0.6)}).

– Dušan Maďar
Nov 22 '18 at 9:43















This sounds like a good idea. If I can make the function return this dictionary then I don't even have to use a queue.

– Shihab Khan
Nov 22 '18 at 9:53





This sounds like a good idea. If I can make the function return this dictionary then I don't even have to use a queue.

– Shihab Khan
Nov 22 '18 at 9:53


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426937%2fusing-globally-declared-tuple-for-python-multiprocessing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin