Python Multiprocessing(TypeError:无法序列化'_io.BufferedReader'对象)

时间:2018-11-09 20:52:09

标签: python-3.x multiprocessing pool

我正在尝试使用Pool对zip文件进行字典攻击以提高速度。 但是我在Python 3.6中遇到下一个错误,尽管它在Python 2.7中有效:

Traceback (most recent call last):
File "zip_crack.py", line 42, in <module>
main()
File "zip_crack.py", line 28, in main
for result in results:
File "/usr/lib/python3.6/multiprocessing/pool.py", line 761, in next
raise value
File "/usr/lib/python3.6/multiprocessing/pool.py", line 450, in               _   handle_tasks
put(task)
File "/usr/lib/python3.6/multiprocessing/connection.py", line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File "/usr/lib/python3.6/multiprocessing/reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
TypeError: cannot serialize '_io.BufferedReader' object

我尝试搜索相同的错误,但是找不到可以在此处提供帮助的答案。

代码看起来像这样

def crack(pwd, f):
   try:
       key = pwd.strip()
       f.extractall(pwd=key)
       return True
   except:
       pass

  z_file = zipfile.ZipFile("../folder.zip")

with open('words.dic', 'r') as passes:
    start = time.time()

    lines = passes.readlines()

    pool = Pool(50)

    results = pool.imap_unordered(partial(crack, f=z_file), lines)
    pool.close()

    for result in results:
        if result:
            pool.terminate()
            break
    pool.join()

我还尝试了另一种使用地图的方法

with contextlib.closing(Pool(50)) as pool:

        pool.map(partial(crack, f=z_file), lines)

效果很好,可以在Python 2.7中快速找到密码,但是在python 3.6中会抛出相同的异常

0 个答案:

没有答案