#Python 3 has had asynchronous capabilities since 3.4. So let's talk about that today!

๐Ÿงต๐Ÿ๐Ÿ‘‡
Your first stop when learning about async in #Python is the official documentation.

There you will learn about the following:

๐Ÿ The `asyncio` module
๐Ÿ The `async` and `await` keywords

docs.python.org/3/library/asynโ€ฆ
The #Python documentation has a nice "Hello World" example for async.

In this example, you see how to create an asynchronous function, sleep and print out some strings:
Just for fun, here's an example of what asynchronous functions looked like back in #Python 3.4

They were explicitly called coroutines and used a decorator rather than the `async def` keywords:
I attempted to learn how to use async calls with #Python a few years ago. Here is an example of scheduling calls to functions using async using the `call_soon()` method.

`call_soon()` calls your function "as soon as it can" using a FIFO queue
The asyncio example in the previous tweet comes from one of my articles @mousevspython here:

blog.pythonlibrary.org/2016/07/26/pytโ€ฆ
There are several other asynchronous frameworks that you can use in #Python

One popular package is Trio:

github.com/python-trio/trโ€ฆ
Before async was brought to the #Python core language, the best way to do it was to use the Twisted package:

twistedmatrix.com/trac/
Another popular #Python async package is Curio from @dabeaz

Check it out sometime:

github.com/dabeaz/curio
I hope you enjoyed learning a little about async in #Python.

Follow me to learn more about Python!

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Mike Driscoll

Mike Driscoll Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @driscollis

8 Nov
Did you know you can watermark images with #Python?

Find out how using the #Pillow package in this mini-thread!

๐Ÿงต๐Ÿ๐Ÿ‘‡
If you want to add text, such as a URL, to an image, you can do that pretty easily with #Python and #pillow

The following code snippet is only 21 lines! Image
Here are the before and after photos of the Yequina lighthouse showing how to add a watermark with #Python ImageImage
Read 5 tweets
8 Nov
Let's take some time and learn about testing in #Python today.

To kick things off, you'll learn about the `doctest` module!

๐Ÿงต๐Ÿ๐Ÿ‘‡
What is `doctest`? #Python's `doctest` modules looks for pieces of text in docstrings that look like REPL sessions.

Next, it executes them and verifies that the output in the docstring matches when the code is actually run

Check out the docs: docs.python.org/3/library/doctโ€ฆ
Here is an example of a #Python function with a docstring that could be used by the `doctest` module to test that function: Image
Read 8 tweets
8 Nov
Have you ever needed to kill โ˜ ๏ธ a subprocess in #Python?

Let's talk a little about that a little!

๐Ÿงต๐Ÿ๐Ÿ‘‡
I had a use-case where I would spin up a process using `subprocess` that I wanted to kill if it ran too long

One way to do that is to use #Python's `threading` module, which has a handy `Timer` class

In this example, the `Timer` class is set to die after 5 seconds Image
#Python's `subprocess` module has a `run()` function that includes a `timeout` parameter (starting in 3.5) that you can use as well.

Here's an example of how that would work: Image
Read 5 tweets
7 Oct
I use a lot of #Python resource. Here are some of my favorite!

Yes, it's another thread!

๐Ÿงต๐Ÿ‘‡๐Ÿ
Mouse vs Python, which is my own blog, is a resource I use myself because I write it for my future self as well as for you.

I put what I am currently working on or neat code snippets on there that I think I will use

blog.pythonlibrary.org
If I need to know about a specific module in #Python, I still check the Python Module of the Week website by @doughellmann

pymotw.com/3/
Read 11 tweets
7 Oct
Concurrency is a hard topic. So today we're going to talk about multiprocessing in #Python

Join for me another fun mini-thread!

๐Ÿงต๐Ÿ‘‡๐Ÿ
The Pros of using `multiprocessing` in #Python (part 1 of 2)

๐Ÿ Processes use separate memory space
๐Ÿ Code can be more straightforward compared to threads
๐Ÿ Uses multiple CPUs / cores
๐Ÿ Avoids the Global Interpreter Lock (GIL)
The Pros of using `multiprocessing` in #Python (part 2 of 2)

๐Ÿ Child processes can be killed (unlike threads)
๐Ÿ The multiprocessing module has an interface similar to `threading.Thread`
๐Ÿ Good for CPU-bound processing (encryption, binary search, matrix multiplication)
Read 9 tweets
7 Oct
Do you know how to make a #Python class method act like an attribute?

You can do that by turning the method into a `property`

Learn how in this thread!

๐Ÿงต๐Ÿ‘‡๐Ÿ
The first step is to look at a regular #Python class.

The following `Person` class has a method called `full_name()`. To get the full name of the `Person`, you must call `full_name()`

Wouldn't it be nice if you could just use dot notation instead of calling it?
In #Python, you can turn the method into a `property` using the `@property` decorator

Once you do that, you can then use dot notation to access the `full_name()` method as if it were an instance attribute!
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(