1

I wrote a very simple script to play around with multiprocessing:

import multiprocessing
import time

def func1(d):
    while d['message'] != 'kill':
        print(d['message'])
    print('done')

def func2(d):
    for i in range(60):
        time.sleep(1)
        d['message'] = 'Message ' + str(i)
    d['message'] = 'kill'

if __name__ == '__main__':
    mgr = multiprocessing.Manager()
    d = mgr.dict()
    d['message'] = 'Waiting...'
    print(d['message'])
    proc1 = multiprocessing.Process(target=func1,args=(d,))
    proc2 = multiprocessing.Process(target=func2,args=(d,))
    proc1.start()
    proc2.start()

When I run this a get the following error:

Process Process-2:
Traceback (most recent call last):
File "C:\Python34\lib\multiprocessing\managers.py", line 724, in _callmethod
conn = self._tls.connection
AttributeError: 'ForkAwareLocal' object has no attribute 'connection'

I've seen some people having similar problems because they've given files the same names as the modules being referenced by the standard library, but I've only written one .py file!

user2357112
  • 260,549
  • 28
  • 431
  • 505
John Hiner
  • 11
  • 4
  • The answer to that post does indeed solve my problem! Thanks, and I'm sorry I missed it. – John Hiner May 10 '17 at 23:15
  • Seems your `Traceback` isn't from your example code. Your code should work, the only thing I want to change is to add a`time.sleep(0.2)` into the `while` of `func1`. – stovfl May 11 '17 at 10:30
  • For anyone looking at this later: My problem was that the parent process was not waiting for execution of proc1 and proc2 to finish. I just called Process.join() on both proc1 and proc2 at the bottom of the script and it worked. – John Hiner May 12 '17 at 14:25

0 Answers0