Multiprocessing throws Attribute Error, why?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Multiprocessing throws Attribute Error, why?



For some reason that I do not see, this code is throwing an error: AttributeError: __exit__


AttributeError: __exit__



The code is simple:


import re, string, math, numpy, time, os.path, itertools, matplotlib, subprocess, shutil, sys, scipy.spatial.distance, multiprocessing, threading, ctypes
from functools import partial
from multiprocessing.sharedctypes import Value, Array
from multiprocessing import Process, Lock

def main():
with multiprocessing.Pool(8) as myPool:
print("1")

if __name__ == '__main__':
main()



The various import lines are for other things I've been using with other code I'm working on multithreading. This however is the simple 'sample' code that I'm trying to analyze to learn the ropes. I assume it's having some sort of trouble opening the with block, but I don't understand why. Does Python 2.7 not implement multiprocessing this way? It's all the examples I've seen. Is there some other way I should be implementing something like this?



I want to be able to throw a bunch of function calls with slightly different inputs to a set of threads and get them back, but that's a far way off if I can't start the threads.





context-management functionality wasn't added until 3.3. It's probably in all the examples you've seen because they are assuming Python 3, which you should probably be using unless you have a good reason not to.
– juanpa.arrivillaga
Jul 31 at 18:18





Some of the code we use is large and difficult to upgrade to Python 3, and some of those here also refuse to upgrade (I am not one of those few). But since my code needs to run for them too it needs to be compatible unfortunately. So I assume that means that I have to use it as an object i.e. obj = multiprocessing.Pool(8) and then later obj.close() to kill it?
– Will
Aug 1 at 14:19


obj = multiprocessing.Pool(8)


obj.close()




1 Answer
1



Some simple reworking to 2.7 standards and this works.


import multiprocessing

def somefunc(x):
print(x)

def main():
myPool = multiprocessing.Pool(8):
myPool.map(range(8))

if __name__ == '__main__':
main()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard