如何在Python中对事件监听器和函数应用多处理?

问题描述:

所以基本上我设置了一个事件监听器,它不断地检查Microsoft Outlook,看看有没有新邮件进入我的收件箱。根据收到的电子邮件,我运行某些过程来检查特定电子邮件是否是我们正在寻找的电子邮件(这不需要太多时间),但是如果电子邮件符合我们的标准,我们会在其上运行某个功能,大约需要一两分钟的时间才能完成。如何在Python中对事件监听器和函数应用多处理?

我的问题是,我正在收到一封电子邮件在同一时间(自动生成),与我需要的标准,我想运行某些功能。当我的程序接收到第一封电子邮件时,它会进入一个电子邮件的过程,大约需要一分钟左右才能完成,此时第二封电子邮件已经进入,而我的事件侦听器已经错过了它。这是一个问题,因为第二封电子邮件对我捕获并运行我的程序也很重要。

我假设多处理它的方式来解决这种情况 - 我怎么去设置一个多处理结构,我的事件侦听器不会停止运行,而功能 - 检查电子邮件是否有效,执行在电子邮件上进行处理 - 可以在收听者仍在移动并捕获下一封电子邮件时继续,然后再次在其上运行该过程。

任何帮助或提示将不胜感激。我正在构建一个衡量指标/分析数据库,为此我收集了许多报告,并自动完成报告过程。

谢谢!


import win32com.client 
import pythoncom 
import time 
import os 

class Handler_Class(object): 
    def OnNewMailEx(self, receivedItemsIDs): 
     # RecrivedItemIDs is a collection of mail IDs separated by a ",". 
     # You know, sometimes more than 1 mail is received at the same moment. 

     for ID in receivedItemsIDs.split(","): 
      print('') 
      print('Running scan...') 
      mail = outlook.Session.GetItemFromID(ID) 
      email_date = mail.SentOn.strftime("%m-%d-%Y" + " at " + "%I:%M:%S %p") 
      email_date_stamp = mail.SentOn.strftime('%m-%d-%Y_at_%I-%M-%S-%p') 
      email_message = mail.Body 
      email_subject = mail.Subject 
      email_sender = mail.SenderEmailAddress 
      email_attachments = mail.Attachments 

      print('From: ' + email_sender) 
      print('Subject: ' + email_subject) 
      print('Date: ' + email_date) 

      try: 
       if check_correct_subject(email_subject) == True: 

         if email_attachments.Count > 0: 
          print(str(email_attachments.Count) + ' attachments found.') 
          for i in range(email_attachments.Count): 
           email_attachment = email_attachments.Item(i + 1) 

           report_name = email_date_stamp + '_' + email_attachment.FileName 
           print(report_name) 
           print('Pushing attachment - ' + report_name + ' - to check_correct_email() function.') 

           if check_correct_attachment(email_attachment) == True: 
            save_incoming_report(email_attachment, report_name, get_report_directory(email_subject)) 

           else: 
            print('Not the attachment we are looking for.') 
            # add error logging here 
            break 

         else: # ***********add error logging here************** 
          print('No attachment found.') 

      except: #add any error logging here# 
       pass 


def check_correct_subject(email_subject): 


def check_correct_attachment(email_attachment): 


def get_report_directory(email_subject): 


def save_incoming_report(email_attachment, report_name, directory): 


def push_email_attachment(email_attachment, report_name, directory): 


def security_risk_report(email_attachment, report_name, directory): 


def broker_risk_report(email_attachment, report_name, directory): 


def forex_data_report(): 



def create_html_security_ldw(df1, df2): 


def send_outlook_email(html_text): 



if __name__ == '__main__': 
    outlook = win32com.client.DispatchWithEvents("Outlook.Application", Handler_Class) 
    #and then an infinite loop that waits from events. 
    pythoncom.PumpMessages() 
+0

问题似乎与您捕捉电子邮件的方式有关,而不是您之后对待他们的方式。向我们展示/解释捕捉电子邮件的代码。事件监听器不应该放弃事件 –

+0

我会编辑自己的帖子 – sgerbhctim

+0

刚刚更新的结构和事件侦听器@CédricJulien – sgerbhctim

你可以找到完成这里的多线程使用多处理模块和Python的队列模块的很好的例子:

Python multiprocessing pool.map for multiple arguments

在该职位的注释提供多示例/代码结构可以适应您的需求。此外,这篇文章解释了为什么您可能会使用多VS主题:

Multiprocessing vs Threading Python

最后,官方库文档多处理这里给你你需要的所有信息:

https://docs.python.org/2/library/multiprocessing.html

+0

我一直在寻找的东西多一点与我的确切情况。有什么机会可以帮助我? – sgerbhctim

+0

嗯,你可以想像它作为: **进口线程 高清funcToRun(X,Y): #事件侦听器的代码,如果任何电子邮件进来 #: T = threading.Thread(目标= funcToRun(),ARGS =(X,Y)) t.start()#开始线程,而无需中断主FN ** 但是这是真的很基本的。更具体的例子类似于您的问题可以在这里找到:http://www.bogotobogo.com/python/Multithread/python_multithreading_Event_Objects_between_Threads.php 希望帮助! – amlaanb

+0

您认为我的问题是针对多处理还是多线程? – sgerbhctim

如何设置多处理结构,为此我的 事件侦听器不会停止运行,而函数

我认为这里的一个明智的方法是使用BaseManager,它将充当您的事件侦听器向其发送Outlook邮件的服务器。然后,所有其他脚本将连接到该服务器并检索需要处理的所有消息。如何做到这一点整齐地解释here