Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Asynchronous Programming in Python

You're reading from   Asynchronous Programming in Python Apply asyncio in Python to build scalable, high-performance apps across multiple scenarios

Arrow left icon
Product type Paperback
Published in Nov 2025
Publisher Packt
ISBN-13 9781836646617
Length 202 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Nicolas Bohorquez Nicolas Bohorquez
Author Profile Icon Nicolas Bohorquez
Nicolas Bohorquez
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Synchronous and Asynchronous Programming Paradigms FREE CHAPTER 2. Identifying Concurrency and Parallelism 3. Generators and Coroutines 4. Implementing Coroutines with Asyncio and Trio 5. Assessing Common Mistakes in Asynchronous Programming 6. Testing and Asynchronous Design Patterns 7. Asynchronous Programming in Django, Flask and Quart 8. Asynchronous Data Access 9. Asynchronous Data Pipelines 10. Asynchronous Computing with Notebooks 11. Unlock Your Exclusive Benefits 12. Other Books You May Enjoy
13. Index

Callbacks, futures and promises

Callbacks are functions that are passed as arguments to other functions or functions that are called inside other functions. These functions can be invoked when an event occurs. Callbacks are usually used as a join point for multithreading/multiprocessing solutions. The following example shows a toy example of a callback function that is invoked from each thread, after the thread has done some processing:

def worker(num, callback):
  print(f"Worker {num} starting...")
  time.sleep(num)  # Simulate some work
  print(f"Worker {num} finished.")
  callback(num)  # Call the callback
def callback_function(num):
  print(f"Callback for worker {num} called.")
if __name__ == "__main__":
  threads = []
  for i in range(5):
    thread = threading.Thread(target=worker, args=(i, callback_function))
    threads.append(thread)
    thread.start()
  for thread in threads:
    thread.join()

Multiprocessing and multithreading callbacks are treated in detail in Anchor 3. While very popular some time ago in other languages, such as JavaScript, this mechanism has some drawbacks that need to be mitigated, including nesting of callbacks, difficult debugging and race conditions. Other mechanisms have been developed to address these and other difficulties, like futures and promises.

The result of an asynchronous call is unknown at the start of the main thread’s execution, and the future/promise concept allows programmers to wait until something is returned before continuing a process. Futures/promises can be awaited up until their execution finishes and can execute a callback function when they end.

Semantics for futures/promises vary by programming language. In Python, futures are part of the standard language API, but promises are implemented by the community based on the Promises/A+ (https://promisesaplus.com/implementations) specification. Futures and promises are covered in detail in Anchor 2.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Asynchronous Programming in Python
You have been reading a chapter from
Asynchronous Programming in Python
Published in: Nov 2025
Publisher: Packt
ISBN-13: 9781836646617
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Modal Close icon
Modal Close icon