I'm using Python in a webpath (CGI for testing, in FastCGI for production), sometimes Need to send email (when a user is registers or something important). Since it takes a lot of time to communicate with the SMTP server, I would like to create a thread for the mail function so that the rest of the applications can end the request without waiting for the email to send.
I tried to use it with thread. Start_new (func, (args)) , but the parent returns return
and before the completion of the sending, before which the sending process has to be executed does anything useful Is there any way to keep the process alive to end the baby process anyway? Take a look at
In fact, it will block your calling suspension until the child thread is back (thus preventing it from exiting it before it).
Update:
Your main thread, which is not responsive to new requests, you can use the loop for a while.
while threading.active_count ()> 0: # ... Check for new requests to handle ... time.sleep (0.1) # or try to add threads to your timeout with thread in #my_threads: # thread.join (0.1 )
Update 2:
It also looks like is thread.start_new (func, args)
. It was updated in thread.start_new_thread (function, args [, kwargs])
You can also create threads with high level threading packages (this is the package that you get to get active_count ()
in the previous code block):
threading my_thread = threading.Thread (target = func, args = (), kwargs = {}) my_thread.daemon = True my_thread .start ()
Comments
Post a Comment