python多进程
高洛峰
高洛峰 2017-04-18 10:12:18
[Python讨论组]

多进程这么配置应该没错吧,可是为什么函数中的两个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 <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: End data with <CR><LF>.<CR><LF>
data: (354, 'End data with <CR><LF>.<CR><LF>')
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 <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: End data with <CR><LF>.<CR><LF>
data: (354, 'End data with <CR><LF>.<CR><LF>')
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]



高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(1)
伊谢尔伦
 p.apply_async(send(msg),args=(i,)) # WRONG 
 ---
 p.apply_async(send, args(i, ))  # RIGHT
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号