Othman
Othman

Reputation: 3018

Does Python Use Threads by Default?

When I write a code using python I always wonder if python uses multithreading by default or not.

Assuming a code such as

l = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print reduce(lambda x, y: x + y, l) / len(l)

Will be done easily using multithreading processes. By letting threads to get the sum of a small collection of the list.

In short, does python use multithreading by default , and when it possible or not? Or always use one ?

Upvotes: 0

Views: 1616

Answers (2)

John La Rooy
John La Rooy

Reputation: 304355

No. Your code will run sequentially.

Additionally your example is relying on your knowledge of the associative property of addition with integers.

Python doesn't know it's a list of integers or that addition will be associative for the objects you are adding.

In particular, if your list is a list of floats, the order you add them up can affect the result

Upvotes: 1

Jeff Ferland
Jeff Ferland

Reputation: 18312

Without explicitly defining threading behavior, Python doesn't use other threads. Further, threading in Python isn't always fully functional because of the Global Interpreter Lock as the interpreter's memory handling isn't thread safe.

Upvotes: 4

Related Questions