找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2201|回复: 0

[求助] 进程启动报错:TypeError: can't pickle _thread.lock objects该如何解决

0

主题

0

帖子

0

积分

贫民

积分
0
alpen 发表于 2019-6-16 17:49:22 | 显示全部楼层 |阅读模式
我查到的原因是因为导入的Queue是queue.Queue所以我便改成了from multiprocessing import Queue,Process
但是依旧报错,因为英语水平有限,无法看懂overflow上的全部回答,所以只能进入论坛问问各位前辈,望帮助
  1. if __name__ == "__main__":

  2.    
  3.         queue1 = Queue()
  4.         queue2 = Queue()
  5.         threadList1 = []
  6.         threadList2 = []
  7.         ipList1 = []
  8.         ipList2 = []
  9.         lockList1 = []
  10.         lockList2 = []
  11.         for i in range(0,10):
  12.             lock1 = threading.Lock()
  13.             lockList1.append(lock1)
  14.             lock2 = threading.Lock()
  15.             lockList2.append(lock2)
  16.         for i in range(1,10):
  17.             lockList1[i].acquire()
  18.             lockList2[i].acquire()
  19.             
  20.         
  21.         p1 = Process(target = startrun, args = (queue1, threadList1, lockList1, ipList1, 0, 200))
  22.         p2 = Process(target = startrun, args = (queue2, threadList2, lockList2, ipList2, 200, 450))
  23.         p1.start()
  24.         p2.start()
  25.         p1.join()
  26.         p2.join()
复制代码
  1. TypeError                                 Traceback (most recent call last)
  2. <ipython-input-13-07db18c7f19b> in <module>()
  3.     218         p1 = Process(target = startrun, args = (queue1, threadList1, lockList1, ipList1, 0, 200))
  4.     219         p2 = Process(target = startrun, args = (queue2, threadList2, lockList2, ipList2, 200, 450))
  5. --> 220         p1.start()
  6.     221         p2.start()
  7.     222         p1.join()

  8. ~\Anaconda3\lib\multiprocessing\process.py in start(self)
  9.     103                'daemonic processes are not allowed to have children'
  10.     104         _cleanup()
  11. --> 105         self._popen = self._Popen(self)
  12.     106         self._sentinel = self._popen.sentinel
  13.     107         # Avoid a refcycle if the target function holds an indirect

  14. ~\Anaconda3\lib\multiprocessing\context.py in _Popen(process_obj)
  15.     221     @staticmethod
  16.     222     def _Popen(process_obj):
  17. --> 223         return _default_context.get_context().Process._Popen(process_obj)
  18.     224
  19.     225 class DefaultContext(BaseContext):

  20. ~\Anaconda3\lib\multiprocessing\context.py in _Popen(process_obj)
  21.     320         def _Popen(process_obj):
  22.     321             from .popen_spawn_win32 import Popen
  23. --> 322             return Popen(process_obj)
  24.     323
  25.     324     class SpawnContext(BaseContext):

  26. ~\Anaconda3\lib\multiprocessing\popen_spawn_win32.py in __init__(self, process_obj)
  27.      63             try:
  28.      64                 reduction.dump(prep_data, to_child)
  29. ---> 65                 reduction.dump(process_obj, to_child)
  30.      66             finally:
  31.      67                 set_spawning_popen(None)

  32. ~\Anaconda3\lib\multiprocessing\reduction.py in dump(obj, file, protocol)
  33.      58 def dump(obj, file, protocol=None):
  34.      59     '''Replacement for pickle.dump() using ForkingPickler.'''
  35. ---> 60     ForkingPickler(file, protocol).dump(obj)
  36.      61
  37.      62 #

  38. TypeError: can't pickle _thread.lock objects
复制代码


而我运用进程的原因是因为之前写好的爬虫爬取豆瓣电影是速度过慢才让我萌生尝试同时运行俩个爬虫的想法,
(之前写的爬虫过慢的原因是因为我不懂得如何有效率的使用付费的ip代理,因为我只学了半个月,所以只能将ip放入list中,用添加删除的方式取得有效ip)

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表