如何在主流程中的“ while循环”中运行其他流程

时间:2019-06-21 07:25:43

标签: python-3.x python-multiprocessing

我想创建面部检测代码,当它在视频供稿中检测到面部时将发送电子邮件。当主要功能检测到人脸时,电子邮件功能需要在后台运行。

在我进行面部检测之前,我试图让电子邮件功能在后台运行(在保存图像功能将图像传递到电子邮件之后)。当我运行代码时,我遇到了2个断言错误:

AssertionError: can only start a process object created by current process

AssertionError: cannot start a process twice

这是我的代码(减去gmail登录名)...

import face_recognition
import cv2
import numpy as np
import time
import datetime

import multiprocessing
import os

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders

def send_email():
    email_user = 'xxxxx@gmail.com'
    email_password = 'xxxxxxxxxxxx'
    email_send = 'xxxxxxx@gmail.com'

    subject = 'Surveillance Project'

    msg = MIMEMultipart()
    msg['From'] = email_user
    msg['To'] = email_send
    msg['Subject'] = subject

    body = 'Hi there, sending this email from Python!'
    msg.attach(MIMEText(body,'plain'))

    filename='joe.jpg'
    print("[INFO] - " + str(datetime.datetime.now()) + " - Image name - " + filename)
    attachment  =open(filename,'rb')

    # create the attachment
    part = MIMEBase('application','octet-stream')
    part.set_payload((attachment).read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition',"attachment; filename= "+filename)
    print("[INFO] - " + str(datetime.datetime.now()) + " - Attachment Created")

    # set up connection
    print("[INFO] - " + str(datetime.datetime.now()) + " - Creating Email to Send")
    msg.attach(part)
    text = msg.as_string()
    print("[INFO] - " + str(datetime.datetime.now()) + " - Setting up connection")
    server = smtplib.SMTP('smtp.gmail.com',587)
    server.starttls()
    server.login(email_user,email_password)

    # send email
    print("[INFO] - " + str(datetime.datetime.now()) + " - Email SENDING" )
    server.sendmail(email_user,email_send,text)
    server.quit()
    print("[INFO] - " + str(datetime.datetime.now()) + " - Email SENT" )

def save_image():
    p1.start()
    print("ID of process p1: {}".format(p1.pid))
    p1.join()
    send_email()

def Main():
    # process IDs
    for i in range(30):
        print(i)
        if i % 10 == 0:
            print("[INFO] - " + str(datetime.datetime.now()) + " - Email initated...")
            save_image()

        # Hit 'q' on the keyboard to quit!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

if __name__ == "__main__":
    # printing main program process id
    print("ID of main process: {}".format(os.getpid()))
    p1 = multiprocessing.Process(target=send_email)
    Main()

1 个答案:

答案 0 :(得分:0)

您正在启动子流程#include <stdio.h> int main() { int i,min,max,num[10]; for(i=0;i<10;i++) { printf("Enter the Numbers : "); scanf("%d", &num[i]); } max=num[0]; for(i=1;i<10;i++) { if(max<num[i]) max=num[i]; else if(min>num[i]) min=num[i]; } printf("Smallest : %d\n", min); printf("Largest : %d\n", max); } ,然后多次启动它。而且您还两次运行p1 ...

考虑以下代码:

send_email