[Python-Dev] Threaded asynchronous return from functions

Aigars Mahinovs aigarius at gmail.com
Mon Jul 4 12:10:57 CEST 2011


Short version: Could we get

def funct( args ):
    if args == 'good':
        return 'good'
    async_return "I'll think about it"
    if args == '123':
        do_x()
    elif args == 'abc':
        do_y()
    else:
        do_z()

as equivalent to

def do_thinking( args ):
    if args == '123':
        do_x()
    elif args == 'abc':
        do_y()
    else:
        do_z()

def funct( args ):
    if args == 'good':
        return 'good'
    t = threading.Thread(target=do_thinking, args=args)
    t.start()
    return "I'll think about it"

Longer version:

I have been doing some multithreaded work lately and have found that
often what I find wanting to do is to call a function, have it check
it's arguments, possibly do some work and then return to the caller,
but still do some extra processing right after that. Currently to
accomplish such feat I need to separate the 'extra processing' bit
into a separate function and call that in a separate thread. A nice
convenience would be a function or statement that would allow to
return a value from the current function, but still keep running its
code (in a separate thread). Such approach could then be used in many
places where async processing is required, such as GUI programming,
XMLRPC, web applications, ... with less boilerplate and more obvious
code flow.

-- 
Best regards,
    Aigars Mahinovs        mailto:aigarius at debian.org
  #--------------------------------------------------------------#
 | .''`.    Debian GNU/Linux (http://www.debian.org)            |
 | : :' :   Latvian Open Source Assoc. (http://www.laka.lv)     |
 | `. `'    Linux Administration and Free Software Consulting   |
 |   `-                                 (http://www.aiteki.com) |
 #--------------------------------------------------------------#


More information about the Python-Dev mailing list