0

I'm trying to use multiprocessing, but I keep getting this error:

AttributeError: Can't get attribute 'processLine' on <module '__main__' 

(The processLine function returns word, so I guess the problem is here, but I don't know how to get around it)

import multiprocessing as mp

pool = mp.Pool(4)
jobs = []
Types =[]

def processLine(line):
     line = line.split()
     word = line[0].strip()
     return word

with open("1.txt", "r", encoding = "utf-8") as f:
     for line in f:
          word = (jobs.append(pool.apply_async(processLine,(line))))
          Types.append(word)
     filtered_words=[]
     with open("2.txt", "r", encoding = "utf-8") as f:
          for line in f:
               word = jobs.append(pool.apply_async(processLine,(line)))
               if word in Types:
                    filtered_words = "".join(line)
     print(filtered_words)

for job in jobs:
    job.get()

pool.close()

And this is what I get: Process ForkPoolWorker-1:

Process ForkPoolWorker-2:

Process ForkPoolWorker-3:

Process ForkPoolWorker-4:

Traceback (most recent call last):

File "/Users/user/anaconda/lib/python3.6/multiprocessing/process.py", line 249, in _bootstrap

self.run()

File "/Users/user/anaconda/lib/python3.6/multiprocessing/process.py", line 93, in run

self._target(*self._args, **self._kwargs)

File "/Users/user/anaconda/lib/python3.6/multiprocessing/pool.py", line 108, in worker

task = get()

File "/Users/user/anaconda/lib/python3.6/multiprocessing/queues.py", line 345, in get return _ForkingPickler.loads(res)

AttributeError: Can't get attribute 'processLine' on

Traceback (most recent call last):

Traceback (most recent call last):

Traceback (most recent call last):

File "/Users/user/anaconda/lib/python3.6/multiprocessing/process.py", line 249, in _bootstrap self.run() File "/Users/user/anaconda/lib/python3.6/multiprocessing/process.py", line 249, in _bootstrap

self.run()

File "/Users/user/anaconda/lib/python3.6/multiprocessing/process.py", line 93, in run

self._target(*self._args, **self._kwargs)

File "/Users/user/anaconda/lib/python3.6/multiprocessing/process.py", line 93, in run

self._target(*self._args, **self._kwargs)

File "/Users/user/anaconda/lib/python3.6/multiprocessing/pool.py", line 108, in worker

task = get()

File "/Users/user/anaconda/lib/python3.6/multiprocessing/pool.py", line 108, in worker task = get() File "/Users/user/anaconda/lib/python3.6/multiprocessing/queues.py", line 345, in get return _ForkingPickler.loads(res) File "/Users/user/anaconda/lib/python3.6/multiprocessing/process.py", line 249, in _bootstrap self.run() File "/Users/user/anaconda/lib/python3.6/multiprocessing/queues.py", line 345, in get return _ForkingPickler.loads(res) File "/Users/user/anaconda/lib/python3.6/multiprocessing/process.py", line 93, in run self._target(*self._args, **self._kwargs) AttributeError: Can't get attribute 'processLine' on AttributeError: Can't get attribute 'processLine' on File "/Users/user/anaconda/lib/python3.6/multiprocessing/pool.py", line 108, in worker task = get()

File "/Users/user/anaconda/lib/python3.6/multiprocessing/queues.py", line 345, in get

return _ForkingPickler.loads(res)

AttributeError: Can't get attribute 'processLine' on

6
  • 1
    If (line) needs to be a tuple, do (line,). A comma makes a tuple, not the parens. Commented Nov 29, 2017 at 2:47
  • No, line doesn't have to be a tuple. Commented Nov 29, 2017 at 2:49
  • Which actual line raises the error? Could you post the full stack trace? Commented Nov 29, 2017 at 3:16
  • Also, I am unable to reproduce your error by just running this in a Py3.6.2 interpreter... Commented Nov 29, 2017 at 3:19
  • What platform (Linux? Windows?) are you running on? Commented Nov 29, 2017 at 3:24

3 Answers 3

4

The multiprocessing module needs to be able to import your module safely. Any code not inside a function or class should be protected by the standard Python import guard:

if __name__ == '__main__':
    ...code goes here...

But there are other problems with your code. For example, you've got:

word = jobs.append(pool.apply_async(processLine,(line)))

...but append doesn't return a value, so this will always assign None to word.

Rather than using a for loop to repeatedly call pool.apply_async, you may want to consider using pool.map_async instead, or just pool.map if you don't actually need the asynchronous behavior.

Sign up to request clarification or add additional context in comments.

3 Comments

Does this explain OP's issue?
Does it not? I mean, obviously I think it does or I wouldn't have bothered posting it here. I suspect that the unprotected code in the file is causing the error, but since it's not a mcve I can't actually try running it myself without futzing around with the code more than I want.
I dunno, was able to run the code fine with made up inputs. Didn't get OP's error or need the guard block.
0

I worked around the AttributeError issue by using VS Code in administrator mode to run it instead of Anaconda Spyder.

Comments

0

I faced the same issue. So, I put all functions in another .py file and imported ipnyb, and called them inside the main function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.