Python 如何为几个线程设置一个imaplib邮箱实例?

Python 如何为几个线程设置一个imaplib邮箱实例?,python,python-3.x,imaplib,Python,Python 3.x,Imaplib,如何在几个不同的线程中为用户设置一个邮箱解析器?每个用户都有2个或更多的公司,他应该解析这些公司的邮件。在我的例子中,我总是有错误 class User(): services = {'company1': 'STOPED', 'company2': 'STOPED'} def __init__(self, user_settings): self.user_name = user_settings['User']

如何在几个不同的线程中为用户设置一个邮箱解析器?每个用户都有2个或更多的公司,他应该解析这些公司的邮件。在我的例子中,我总是有错误

class User():
    services = {'company1': 'STOPED',
                'company2': 'STOPED'}

    def __init__(self, user_settings):
        self.user_name = user_settings['User']
        self.user_settings = user_settings

        self.global_mailbox = None

    def company1(self):
        service_name = 'company1'
        settings = self.user_settings['Company1']
        gb_mail = False

        def mailbox_check(mailbox):
            nonlocal settings, service_name

            mailbox.noop()
            status, mails = mailbox.search(None, '(UNSEEN)', '(FROM "company1@cmp.com")')

            ....

        if not settings['Mail Login']:
            if self.global_mailbox:
                mailbox = self.global_mailbox
                gb_mail = True
            else:
                self.SET_GLOBAL_MAIL()
                if not self.global_mailbox:
                    return
                else:
                    mailbox = self.global_mailbox
        else:
            mail_host = 'imap.{0}'.format(settings['Mail Login'].split('@')[-1])
            mail_login = settings['Mail Login']
            mail_password = settings['Mail Password']

            mailbox = imaplib.IMAP4_SSL(mail_host)
            mailbox.sock.settimeout(43200)

            mailbox.login(mail_login, mail_password)
            mailbox.select('INBOX')
            gb_mail = False

         while self.services[service_name] != 'STOPED':
             time.sleep(5)
             new_orders = self.mailbox_check(mailbox)
             if new_orders:
                 action()    
             if self.services[service_name] == 'STOPED':
                 break

    def company2(self):
        service_name = 'company2'
        settings = self.user_settings['Company2']
        gb_mail = False

        def mailbox_check(mailbox):
            nonlocal settings, service_name

            mailbox.noop()
            status, mails = mailbox.search(None, '(UNSEEN)', '(FROM "company2@cmp.com")')

            ....

        if not settings['Mail Login']:
            if self.global_mailbox:
                mailbox = self.global_mailbox
                gb_mail = True
            else:
                self.SET_GLOBAL_MAIL()
                if not self.global_mailbox:
                    return
                else:
                    mailbox = self.global_mailbox
        else:
            mail_host = 'imap.{0}'.format(settings['Mail Login'].split('@')[-1])
            mail_login = settings['Mail Login']
            mail_password = settings['Mail Password']

            mailbox = imaplib.IMAP4_SSL(mail_host)
            mailbox.sock.settimeout(43200)

            mailbox.login(mail_login, mail_password)
            mailbox.select('INBOX')
            gb_mail = False

         while self.services[service_name] != 'STOPED':
             time.sleep(5)
             new_orders = self.mailbox_check(mailbox)
             if new_orders:
                 action()    
             if self.services[service_name] == 'STOPED':
                 break

def SET_GLOBAL_MAIL(self)
    try:
        gb_mail = self.user_settings['Global Mail']
        mail_host = 'imap.{0}'.format(gb_mail['Login'].split('@')[-1])
        mail_login = gb_mail['Login']
        mail_password = gb_mail['Password']
        mailbox = imaplib.IMAP4_SSL(mail_host)
        mailbox.sock.settimeout(43200)
        mailbox.login(mail_login, mail_password)
        mailbox.select('INBOX')
        self.global_mailbox = mailbox
    except:
        self.global_mailbox = None
        pass

def START_ALL(self):
    self.SET_GLOBAL_MAIL()
    for service in self.services.keys():
        self.services[service] = 'RUNNING'
        threading.Thread(target=lambda: self.__getattribute__(service)(), name='{0} [{1} service thread]'.format(self.user_name, service), daemon=True).start()

>>>user = User(settings)
>>>user.START_ALL()
几秒钟后,我发现以下错误:

imaplib.IMAP4.abort:command:SEARCH=>socket错误:EOF

imaplib.IMAP4.abort:套接字错误:EOF

imaplib.IMAP4.abort:command:NOOP=>socket错误:EOF

imaplib.IMAP4.abort:套接字错误:[WinError 10014]系统检测到 尝试在调用中使用指针参数时指针地址无效

ssl.SSLError:[ssl:SSLV3\u ALERT\u BAD\u RECORD\u MAC]SSLV3 ALERT BAD RECORD MAC(\u ssl.c:2273)


如果我正在为每个线程创建一个新的imap会话,所有这些都可以正常工作,但是GMAIL对通过imap同时连接有一个限制。用户可能有超过15家公司进行解析。如何为用户的所有操作设置一个全局邮件?

在多个对话中使用相同的IMAP连接是没有意义的,因为只有一个套接字连接和一个服务器端上下文。如果一个线程请求mailbox1,另一个线程请求mailbox2,服务器将依次选择这两个邮箱,并停留在最后一个邮箱中

如果两个线程同时从同一个套接字读取数据,则不讨论争用条件:每个线程只获取准随机部分数据,而另一个线程将读取另一部分数据。很抱歉,这是不可能的