0

There is function which need to sleep 10 second. But in the meantime, I don't want it to block my main function. How to deal with that? I try to do like that. However, it still block my main function.

def tester():
   pool.size=2;
   pool=multiprocessing.Pool(pool_size);
   pool.apply_async(send_mail, args=("test",));
   pool.close();
   pool.join();
   print "email is done";

the main function will call this function.

2 Answers 2

1

pool.join();

is your blocker. remove it to not block.

https://docs.python.org/2/library/multiprocessing.html#multiprocessing.Process.join

EDIT:

import threading 

print "start"
th = threading.Thread(target=tester)
th.daemon = False
print "thread start"
th.start()
print "executing other code"
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, it works. The reason it doesn't show up is it seems like the main function end before the subprocess finish.
spin off your emails into a separate thread. by marking it as non-daemon the python program will not exit until it is finished. i added some sample code above
0

Not sure why you are using Pool. You are spawning 2 subprocesses to send a single email? What about just spawning a single process. The following should get you started. (I put a "sleep(30)" where you would actually call send_mail)

from multiprocessing import Process, Queue
from time import sleep

def wrap_send_email(q, msg):
    try:
        sleep(30)
        q.put( [ msg, 'mail sent sucessfully'] )
    except: 
        q.put( [ msg, 'mail not sent successfully'] )


def test():
    q = Queue()
    p = Process(target=wrap_send_email, args=(q, 'test message'))
    p.start()
    while p.is_alive():
        print("Doing stuff while the other process is working.")
        sleep(5)
    print(q.get())
    p.join()

6 Comments

The reason of blocking is join().
Yes. I was perhaps misunderstanding what you were trying to achieve. You have the statement, print "email is done" after the join. If you remove the join, that statement will execute immediately and not necessarily after the email is done. I also am not sure why you need a pool of 2 for this application. A pool of 1 seems sufficient, in which case, why use a Pool at all, just use a Process.
You are right. I should set pool as 1. What if there is a function stuck in the pool? I am curious. Will it go to the 2nd pool if I set pool as 2?
A Pool is just a way of distributing work to multiple worker Processes and gathering back up the results. I'm not sure what you mean by "stuck", but there is no fail-over type functionality between processes in a Pool. Changing the Pool to 1 will work, but in my opinion defeats the purpose of using Pool in the first place. Just use Process.
I mean block or break in the pool. And also what if I want to keep the child task in the pool and follow the query. The one need to wait the one ahead of it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.