python多进程

 胖儿的小金子 发布于 2022-10-28 20:14

多进程这么配置应该没错吧,可是为什么函数中的两个print都没有执行呢,下面是输出,删除了个人信息,只打出了parent process 的pid

from email.mime.text import MIMEText
import smtplib
from multiprocessing import Pool
import os, time, random

def send(a):
    print 'Run task %s (%s)...' % (a, os.getpid())
    from_addr = 'com'
    password = 'L'
    smtp_server = '.com'
    to_addr = '.com'
    server = smtplib.SMTP(smtp_server, 25)
    server.set_debuglevel(1)
    server.login(from_addr, password)
    server.sendmail(from_addr, [to_addr], a.as_string())
    server.quit()
    start = time.time()
    time.sleep(random.random() * 3)
    end = time.time()
    print 'Task %s runs %0.2f seconds.' % (a, (end - start))
if __name__ == '__main__':
    print 'Parent process %s.' % os.getpid()
    p = Pool()
    for i in range(2):
        msg = MIMEText(str(i), 'plain', 'utf-8')
        p.apply_async(send(msg),args=(i,))
    print 'Waiting for all subprocesses done...'
    p.close()
    p.join()
    print 'All subprocesses done.'
Parent process 21848.
Run task From nobody Sat Jan 07 11:30:26 2017
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

MA==
 (21848)...
send: 'ehlo []\r\n'
reply: '250-\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-SIZE 104857600\r\n'
reply: '250-VRFY\r\n'
reply: '250-ETRN\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-AUTH LOGIN PLAIN\r\n'
reply: '250-AUTH=LOGIN PLAIN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-8BITMIME\r\n'
reply: '250 DSN\r\n'
reply: retcode (250); Msg: 
PIPELINING
SIZE 104857600
VRFY
ETRN
STARTTLS
AUTH LOGIN PLAIN
AUTH=LOGIN PLAIN
ENHANCEDSTATUSCODES
8BITMIME
DSN
send: 'AUTH PLAIN GxlZnU4LmNvbQBMZHozOTM3NjUxNTE=\r\n'
reply: '235 2.7.0 Authentication successful\r\n'
reply: retcode (235); Msg: 2.7.0 Authentication successful
send: 'mail FROM:<.com> size=100\r\n'
reply: '250 2.1.0 Ok\r\n'
reply: retcode (250); Msg: 2.1.0 Ok
send: 'rcpt TO:<.com>\r\n'
reply: '250 2.1.5 Ok\r\n'
reply: retcode (250); Msg: 2.1.5 Ok
send: 'data\r\n'
reply: '354 End data with .\r\n'
reply: retcode (354); Msg: End data with .
data: (354, 'End data with .')
send: 'Content-Type: text/plain; charset="utf-8"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: base64\r\n\r\nMA==\r\n.\r\n'
reply: '250 2.0.0 Ok: queued as 5ED25480640\r\n'
reply: retcode (250); Msg: 2.0.0 Ok: queued as 5ED25480640
data: (250, '2.0.0 Ok: queued as 5ED25480640')
send: 'quit\r\n'
reply: '221 2.0.0 Bye\r\n'
reply: retcode (221); Msg: 2.0.0 Bye
Task From nobody Sat Jan 07 11:30:38 2017
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

MA==
 runs 1.13 seconds.
Run task From nobody Sat Jan 07 11:30:38 2017
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

MQ==
 (21848)...
send: 'ehlo []\r\n'
reply: '250-\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-SIZE 104857600\r\n'
reply: '250-VRFY\r\n'
reply: '250-ETRN\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-AUTH LOGIN PLAIN\r\n'
reply: '250-AUTH=LOGIN PLAIN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-8BITMIME\r\n'
reply: '250 DSN\r\n'
reply: retcode (250); Msg: 
PIPELINING
SIZE 104857600
VRFY
ETRN
STARTTLS
AUTH LOGIN PLAIN
AUTH=LOGIN PLAIN
ENHANCEDSTATUSCODES
8BITMIME
DSN
send: 'AUTH PLAIN ozOTM3NjUxNTE=\r\n'
reply: '235 2.7.0 Authentication successful\r\n'
reply: retcode (235); Msg: 2.7.0 Authentication successful
send: 'mail FROM:<> size=100\r\n'
reply: '250 2.1.0 Ok\r\n'
reply: retcode (250); Msg: 2.1.0 Ok
send: 'rcpt TO:<>\r\n'
reply: '250 2.1.5 Ok\r\n'
reply: retcode (250); Msg: 2.1.5 Ok
send: 'data\r\n'
reply: '354 End data with .\r\n'
reply: retcode (354); Msg: End data with .
data: (354, 'End data with .')
send: 'Content-Type: text/plain; charset="utf-8"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: base64\r\n\r\nMQ==\r\n.\r\n'
reply: '250 2.0.0 Ok: queued as D0183480640\r\n'
reply: retcode (250); Msg: 2.0.0 Ok: queued as D0183480640
data: (250, '2.0.0 Ok: queued as D0183480640')
send: 'quit\r\n'
reply: '221 2.0.0 Bye\r\n'
reply: retcode (221); Msg: 2.0.0 Bye
Task From nobody Sat Jan 07 11:30:51 2017
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

MQ==
 runs 0.88 seconds.
Waiting for all subprocesses done...
All subprocesses done.
[Finished in 25.0s]



1 个回答
  •  p.apply_async(send(msg),args=(i,)) # WRONG 
     ---
     p.apply_async(send, args(i, ))  # RIGHT
    2022-10-29 22:12 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有