Python &引用;TypeError:无法pickle';SSL对象'&引用;将concurrent.futuresProcessPoolExecutor()与IMAP一起使用时
我将Python3.9与IMAPlib结合使用,以便检索电子邮件并从中获取链接。它工作得很好,但对于大量的电子邮件来说,速度可能会非常慢(我正在处理大约40000封)。为了加快速度,我想使用一些并发性,我可以一次收到所有的电子邮件 为此,我事先获取所有电子邮件的ID,然后将每个ID分配给我池中的任务。在调用scrape_link_mp()之前,我关闭了以前使用的impalib连接。我曾尝试使用锁和管理器锁,但仍然出现相同的错误 我是不是错过了一些基本的东西?如果还有什么需要解释的,请告诉我,谢谢 我的代码如下所示:Python &引用;TypeError:无法pickle';SSL对象'&引用;将concurrent.futuresProcessPoolExecutor()与IMAP一起使用时,python,python-multiprocessing,concurrent.futures,imaplib,Python,Python Multiprocessing,Concurrent.futures,Imaplib,我将Python3.9与IMAPlib结合使用,以便检索电子邮件并从中获取链接。它工作得很好,但对于大量的电子邮件来说,速度可能会非常慢(我正在处理大约40000封)。为了加快速度,我想使用一些并发性,我可以一次收到所有的电子邮件 为此,我事先获取所有电子邮件的ID,然后将每个ID分配给我池中的任务。在调用scrape_link_mp()之前,我关闭了以前使用的impalib连接。我曾尝试使用锁和管理器锁,但仍然出现相同的错误 我是不是错过了一些基本的东西?如果还有什么需要解释的,请告诉我,谢谢
def scrape_link_mp(self):
self.file_counter = 0
self.login_session.logout()
self.Manager = multiprocessing.Manager()
self.lock = self.Manager.Lock()
futures = []
with concurrent.futures.ProcessPoolExecutor() as Executor:
for self.num_message in self.arr_of_emails[self.start_index:]:
task_params = self.current_user,self.current_password,self.counter,self.imap_url,self.num_message,self.substring_filter,self.link_regex,self.lock
futures.append(
Executor.submit(
self.scrape_link_from_email_single,
*task_params
)
)
for future in concurrent.futures.as_completed(futures):
self.counter+=1
self.timestamp = time.strftime('%H:%M:%S')
print(f'[{self.timestamp}] DONE: {self.counter}/{len(self.num_mails)}')
print(future.result())
def scrape_link_from_email_single(self,current_user,current_password,counter,imap_url,num_message,substring_filter,link_regex,lock):
login_session_mp.logout()
current_user_mp = self.current_user
current_password_mp = self.current_password
self.lock.acquire()
login_session_mp = imaplib.IMAP4_SSL(self.imap_url,993)
login_session_mp.login(current_user_mp,current_password_mp)
self.search_mail_status, self.amount_matching_criteria = login_session_mp.search(Mail.CHARSET,search_criteria)
_,individual_response_data = login_session_mp.fetch(self.num_message,'(RFC822)')
self.lock().release
raw = email.message_from_bytes(individual_response_data[0][1])
scraped_email_value = str(email.message_from_bytes(Mail.scrape_email(raw)))
print(scraped_email_value)
returned_links = str(link_regex.findall(scraped_email_value))
for i in returned_links:
if substring_filter:
self.lock.acquire()
with open('out.txt','a+') as link_file:
link_file.write(i +'\n')
link_file.close()
self.lock.release()