Is there a generic notion of asynchronous programming in python? Could I assign a callback to a function, execute it and return to the main program flow immediately, no matter how long the execution of that function would take?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You may see my Python Asynchronous Programming tool: http://www.ideawu.com/blog/2010/08/delegate-in-pythonpython-asynchronous-programming.html
You may well want to checkout the Twisted library for Python. They provide many useful tools.
Good news everyone!
Python 3.4 would include brand new ambitious asynchronous programming implementation!
It is currently called tulip and already has an active following.
As described in PEP 3153: Asynchronous IO support and PEP 3156: Asynchronous IO Support Rebooted:
Here is the brief overview of it's abilities.
What you describe (the main program flow resuming immediately while another function executes) is not what's normally called "asynchronous" (AKA "event-driven") programming, but rather "multitasking" (AKA "multithreading" or "multiprocessing"). You can get what you described with the standard library modules
threading
andmultiprocessing
(the latter allows actual concurrent execution on multi-core machines).Asynchronous (event-driven) programming is supported in the standard Python library in the
asyncore
andasynchat
modules, which are very oriented to networking tasks (indeed they internally use theselect
module, which, on Windows, only supports sockets -- though on Unixy OSs it can also support any file descriptor).For a more general (though also mostly networking oriented, but not limited to that) support for asynchronous (event-driven) programming, check out the twisted third-party package.
Take a look here:
Asynchronous Programming in Python
An Introduction to Asynchronous Programming and Twisted
Worth checking out:
asyncio (previously Tulip) has been checked into the Python default branch
Edited on 14-Mar-2018
Today Python has asyncIO — Asynchronous I/O, event loop, coroutines and tasks built in.
Description taken from the link above:
Also worth checking out:
A guide to asynchronous programming in Python with asyncIO
The other respondents are pointing you to Twisted, which is a great and very comprehensive framework but in my opinion it has a very un-pythonic design. Also, AFAICT, you have to use the Twisted main loop, which may be a problem for you if you're already using something else that provides its own loop.
Here is a contrived example that would demonstrate using the
threading
module:However, in pretty much every useful case, you will want to communicate between threads. You should look into synchronization primitives, and become familiar with the concept of concurrency and the related issues.
The
threading
module provides many such primitives for you to use, if you know how to use them.