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)
The Cons of using `multiprocessing in #Python

๐Ÿ Interprocess communication is more complicated
๐Ÿ Memory footprint is larger than threads
Here is a small example of using `multiprocessing` in #Python

In this example, you use a `for` loop to create multiple `Process` instances that target the `doubler()` function.

Then you `start()` the processes. The output is in the second screenshot
You can name your processes in #Python. This makes debugging easier since each process has a specific name
You can add locks to your processes in #Python so that the processes don't try to access the same thing at the same time:
If you'd like to learn more about `multiprocessing` in #Python, check out my tutorial @mousevspython

blog.pythonlibrary.org/2016/08/02/pytโ€ฆ
I hope you enjoyed learning about the basics of `multiprocessing` in #Python with me. Be sure to share this thread with your friends and follow me for more Python tips!

โ€ข โ€ข โ€ข

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

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
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
6 Oct
There are a lot of "magic methods" or "dunder methods" in #Python.

I'll be talking about these for a few days, so stay tuned!

First off is `__init__()` and `__repr__()`
A magic method is a #Python class method that begins and ends with double-underscores

Most classes require you to create an `__init__()`

`__init__()` is short for initialization and initializes the instance
Sometimes, `__init__()` is called a constructor. However, I have seen it claimed that you need to combine `__new__()` with `__init__()` for it to be a true constructor

`__new__()` is used for allocating memory while `__init__()` is used for initializing the instance
Read 7 tweets
6 Oct
When using a class in #Python, you may notice that the first argument in all the class's methods is "self"

What is up with that? Well, let's talk about it!

๐Ÿงต๐Ÿ‘‡๐Ÿ
First of all, "self" is a convention that refers to the instance that is created when you instantiate a class. You don't have to name it "self", but that is best practice

Here is an example where "self" is replaced with "this"
You use "self" to be able to access instance attributes across methods in a #Python class.

Here is an example where you initialize (__init__) `self.name` and then use `self.name` later on when you call the `walk()` method:
Read 5 tweets
6 Oct
Whenever you talk about classes in #Python, you will probably see the `super()` function mentioned.
The most common way of writing `super()` in a #Python class is to write it like this:
You can also call `super()` with the class name and self passed in, but most #Python developers see that as redundant since it is the equivalent of `super().__init__()`
Read 5 tweets
6 Oct
Today I'm going to start a series on #Python classes.

According to the Python docs: "Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. "

๐Ÿงต๐Ÿ‘‡๐Ÿ
Here is an example of a #Python class that has a single attribute: `name`

๐Ÿ `animal_1` is an instance of the `Animal()` class
๐Ÿ `animal_2` is another instance of the `Animal()` class

You can access an instance's attributes using the "." operator
A #Python class always inherits from the `object` base class. In Python 3, you do not need to specify that you inheriting from `object`

That means that the following two examples are doing the same thing:
Read 4 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!

:(