0

test.py and main.py is in the same folder, and I execute the main.py, the Interpreter report the following error.

Python environment:python2.7.15

how to fix this issue?

Google told me that this issue is fixed in python3.4, but I want to use this style in python 2.7, how to do it test.py

from multiprocessing import Process, Queue
def interaction(q):
    print '*'*50
    q.put('-'*50)
q = Queue()

p = Process(target=interaction, args=(q,))
p.start()
p.join()
print q.get()

main.py

if __name__ == '__main__':

    execfile('test.py')

Error:

Traceback (most recent call last):
  File "C:\sync360\code\python\oobe_test\multiprocessing_test\demorund\main.py", line 3, in <module>
    execfile('test.py')
  File "test.py", line 13, in <module>
    main()
  File "test.py", line 10, in main
    p.start()
  File "C:\Python27\lib\multiprocessing\process.py", line 130, in start
    self._popen = Popen(self)
  File "C:\Python27\lib\multiprocessing\forking.py", line 277, in __init__
    dump(process_obj, to_child, HIGHEST_PROTOCOL)
  File "C:\Python27\lib\multiprocessing\forking.py", line 199, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "C:\Python27\lib\pickle.py", line 224, in dump
    self.save(obj)
  File "C:\Python27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "C:\Python27\lib\pickle.py", line 425, in save_reduce
    save(state)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 687, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 754, in save_global
    (obj, module, name))
pickle.PicklingError: Can't pickle <function func at 0x02B3C1B0>: it's not found as __main__.func
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python27\lib\multiprocessing\forking.py", line 381, in main
    self = load(from_parent)
  File "C:\Python27\lib\pickle.py", line 1384, in load
    return Unpickler(file).load()
  File "C:\Python27\lib\pickle.py", line 864, in load
    dispatch[key](self)
  File "C:\Python27\lib\pickle.py", line 886, in load_eof
    raise EOFError
EOFError
[Finished in 0.3s with exit code 1]
jsplyy
  • 59
  • 6
  • Possible duplicate of [Python multiprocessing pickling error](https://stackoverflow.com/questions/8804830/python-multiprocessing-pickling-error) – Alexander McFarlane Sep 11 '18 at 12:17
  • Try with adding `from test import interaction` in your main.py, then in test.py before `q = Queue()` add `if __name__ == '__main__':` and add the appropriate indent. – Darkonaut Sep 12 '18 at 00:02

1 Answers1

0

My hunch is the usage of execfile() is causing this, as it's confusing Python as to what is __main__.

You could instead import the test module (which, btw, should be renamed to something more descriptive).

AKX
  • 152,115
  • 15
  • 115
  • 172
  • my code could be only invoked with exefile() in the whole system. Any workaround could let me use multiprocessing in execfile()? – jsplyy Sep 11 '18 at 12:22
  • Why can't it be invoked without `execfile()`? That sounds rather strange. – AKX Sep 11 '18 at 12:24
  • The whole software system is not controlled by myself, The architecture only provide the interface for user to write an interact.py file to do something. – jsplyy Sep 11 '18 at 12:28