in Education by
Good day to you, Today I was moving code from threading to multiprocess. Everything seemed okay, until I got The following error: Error Traceback (most recent call last): File "run.py", line 93, in main() File "run.py", line 82, in main emenu.executemenu(components, _path) File "/home/s1810979/paellego/lib/execute/execute_menu.py", line 29, in executemenu e.executeall(installed, _path) File "/home/s1810979/paellego/lib/execute/execute.py", line 153, in executeall pool.starmap(phase2, args) File "/usr/lib64/python3.4/multiprocessing/pool.py", line 268, in starmap return self._map_async(func, iterable, starmapstar, chunksize).get() File "/usr/lib64/python3.4/multiprocessing/pool.py", line 608, in get raise self._value File "/usr/lib64/python3.4/multiprocessing/pool.py", line 385, in _handle_tasks put(task) File "/usr/lib64/python3.4/multiprocessing/connection.py", line 206, in send self._send_bytes(ForkingPickler.dumps(obj)) File "/usr/lib64/python3.4/multiprocessing/reduction.py", line 50, in dumps cls(buf, protocol).dump(obj) _pickle.PicklingError: Can't pickle : attribute lookup module on builtins failed Code execute.py def executeall(components, _path): args = [] manager = multiprocessing.Manager() q = manager.Queue() resultloc = '/some/result.log' for component in components: for apkpath, resultpath in zip(execonfig.apkpaths, execonfig.resultpaths): args.append((component,apkpath,resultpath,q,)) #Args for subprocesses cores = askcores() with multiprocessing.Pool(processes=cores) as pool: watcher = pool.apply_async(lgr.log, (resultloc+'/results.txt', q,)) pool.starmap(phase2, args) component.py class Component(object): def __init__(self, installmodule, runmodule, installerloc, installationloc, dependencyloc): self.installmodule = installmodule self.runmodule = runmodule self.installerloc = installerloc self.installationloc = installationloc self.dependencyloc = dependencyloc self.config = icnf.Installconfiguration(installerloc+'/conf.conf') #lots of functions... installconfig.py class State(Enum): BEGIN=0 #Look for units UNIT=1 #Look for unit keypairs KEYPAIR=3 class Phase(Enum): NONE=0 DEPS=1 PKGS=2 class Installconfiguration(object): def __init__(self, config): dictionary = self.reader(config) #Fill a dictionary #dictionary (key:Phase, value: (dictionary key: str, job)) self.deps = dictionary[Phase.DEPS] self.pkgs = dictionary[Phase.PKGS] job.py class Job(object): def __init__(self, directory=None, url=None): self.directory = directory if directory else '' self.url = url if url else '' As you can see, I pass a component as argument to function phase2(component, str, str, multiprocess.manager.Queue()). The second and third argument of the constructor of component are modules imported with importlib. What I tried I am new to python, but not to programming. Here is what I tried: Because the error itself did not point out what the problem was exactly, I tried removing args to find out which can't be pickled: Remove component, and everything works fine, so this appears to be the cause for trouble. However, I need this object passed to my processes. I searched around the internet for hours, but did not find anything but basic tutorials about multiprocessing, and explanations about how pickle works. I did find this saying it should work, but not on windows or something. However, it does not work on Unix (which I use) My ideas As I understood it, nothing suggests I cannot send a class containing two importlib modules. I do not know what the exact problem is with component class, but importlib module as members are the only non-regular things. This is why I believe the problem occurs here. Question Do you know why a class containing modules is unsuitable for 'pickling'? How can one get a better idea why and where Can't pickle errors occur? More code Full source code for this can be found on https://github.com/Sebastiaan-Alvarez-Rodriguez/paellego Questions to me Please leave comments requesting clarifications/more code snippets/??? if you would like me to edit this question A last request I would like solutions to use python standard library only, python 3.3 preferably. Also, a requirement of my code is that it runs on Unix systems. Thanks in advance Edit As requested, here is a minimal example which greatly simplifies the problem: main.py (you could execute as python main.py foo) #!/usr/bin/env python import sys import importlib import multiprocessing class clazz(object): def __init__(self, moduly): self.moduly = moduly def foopass(self, stringy): self.moduly.foo(stringy) def barpass(self, stringy, numbery): self.moduly.bar(stringy) print('Second argument: '+str(numbery)) def worker(clazzy, numbery): clazzy.barpass('wow', numbery) def main(): clazzy = clazz(importlib.import_module(sys.argv[1])) clazzy.foopass('init') args = [(clazzy, 2,)] with multiprocessing.Pool(processes=2) as pool: pool.starmap(worker, args) if __name__ == "__main__": main() foo.py (needs to be in same directory for above call suggestion): #!/usr/bin/env python globaly = 0 def foo(stringy): print('foo '+stringy) global globaly globaly = 5 def bar(stringy): print('bar '+stringy) print(str(globaly)) This gives error upon running: TypeError: can't pickle module objects Now we know that pickling module objects is (sadly) not possible. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
In order to get rid of the error, let clazz not take a module as attribute, however convenient, but let it take "modpath", which is the required string for importlib to import the module specified by user. It looks like this (foo.py remains exactly the same as above): #!/usr/bin/env python import sys import importlib import multiprocessing class clazz(object): def __init__(self, modpathy): self.modpathy = modpathy def foopass(self, stringy): moduly = importlib.import_module(self.modpathy) moduly.foo(stringy) def barpass(self, stringy, numbery): moduly = importlib.import_module(self.modpathy) moduly.bar(stringy) print('Second argument: '+str(numbery)) def worker(clazzy, number): clazzy.barpass('wow', number) def main(): clazzy = clazz(sys.argv[1]) clazzy.foopass('init') args = [(clazzy, 2,)] with multiprocessing.Pool(processes=2) as pool: pool.starmap(worker, args) if __name__ == "__main__": main() If you require that your globals, such as globaly, are guaranteed to maintain state, then you need to pass a mutable object (e.g. list, dictionary) to hold this data, thanks @DavisHerring: Module attributes are called “global variables” in Python, but they are no more persistent or accessible than any other data. Why not just use dictionaries? The example code would look like this: #!/usr/bin/env python import sys import importlib import multiprocessing class clazz(object): def __init__(self, modpathy): self.modpathy = modpathy self.dictionary = {} def foopass(self, stringy): moduly = importlib.import_module(self.modpathy) moduly.foo(stringy, self.dictionary) def barpass(self, stringy, numbery): moduly = importlib.import_module(self.modpathy) moduly.bar(stringy, self.dictionary) print('Second argument: '+str(numbery)) def worker(clazzy, number): clazzy.barpass('wow', number) def main(): clazzy = clazz(sys.argv[1]) clazzy.foopass('init') args = [(clazzy, 2,)] with multiprocessing.Pool(processes=2) as pool: pool.starmap(worker, args) if __name__ == "__main__": main() foo.py (no more globals): #!/usr/bin/env python def foo(stringy, dictionary): print('foo '+stringy) globaly = 5 dictionary['globaly'] = globaly def bar(stringy, dictionary): print('bar '+stringy) globaly = dictionary['globaly'] print(str(globaly)) This way you can work around the problem without annoying can't pickle ... errors, and while maintaining states

Related questions

0 votes
    I have a dictionary d full of a collection of dataframes: key type size value gm1 dataframe mxn .. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I've created a python3 script that runs fine on command line but when I try to run as a daemon ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I have a legit situation like this where I need to implement a method that changes the incoming object ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    i want to create a new layer using scapy,i created a new layer but when i sent it to another ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I am trying to write a TDMA algorithm with numba in nopython mode. Here is my code: @jit(nopython ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    When I run my Dash app from the command line using export PYTHONPATH=./src/ && python ./src/pdv/ ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    I want to log the user session. Currently the code is as follows (setting formatter and handlers is omitted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    I want to log the user session. Currently the code is as follows (setting formatter and handlers is omitted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I want to log the user session. Currently the code is as follows (setting formatter and handlers is omitted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    I want to log the user session. Currently the code is as follows (setting formatter and handlers is omitted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    I am reading a yaml file like so in Python3: def get_file(): file_name = "file.yml" properties_stream ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have the following data-frame: -- dcc.Dropdown for selecting the issue goes here on the page -- ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How are lambdas useful? [closed] (26 answers) Closed 3 years ago ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 15, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How are lambdas useful? [closed] (26 answers) Closed 3 years ago ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
...