python多进程

查看:121
本文介绍了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]



解决方案

 p.apply_async(send(msg),args=(i,)) # WRONG 
 ---
 p.apply_async(send, args(i, ))  # RIGHT

这篇关于python多进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆