Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Playing With Threads and Python

DZone 's Guide to

Playing With Threads and Python

Decrease runtime with threads in Python.

· Big Data Zone ·
Free Resource

Today, I want to play a little bit with Python and threads. I don’t like to use threads; they are very powerful but, as uncle Ben said, "Great power comes great responsibility." I prefer a decouple process with queues instead of using threads, but sometimes I need to use them. Let’s start.

First, I will build a simple script without threads. This script will append three numbers (one, two, and three) to a list and add them together. The function that appends numbers to the list sleeps a number of seconds equal to the number that we’re appending.

import time

start_time = time.time()

total = []


def sleeper(seconds):
    time.sleep(seconds)
    total.append(seconds)


for s in (1, 2, 3):
    sleeper(s)

end_time = time.time()

total_time = end_time - start_time

print("Total time: {:.3} seconds. Sum: {}".format(total_time, sum(total)))


The outcome of the script is obviously six, and, as we’re sleeping the script a number of seconds equal to the final output, it takes six seconds to finish.

Now, we’re going to use a threading version of the script. We’re going to perform the same operations as in the first script, but we’re going to use one thread for each append (with the same sleep function).

import time
from queue import Queue
import threading

start_time = time.time()

queue = Queue()


def sleeper(seconds):
    time.sleep(seconds)
    queue.put(seconds)


threads = []
for s in (1, 2, 3):
    t = threading.Thread(target=sleeper, args=(s,))
    threads.append(t)
    t.start()

for one_thread in threads:
    one_thread.join()

total = 0
while not queue.empty():
    total = total + queue.get()

end_time = time.time()

total_time = end_time - start_time
print("Total time: {:.3} seconds. Sum: {}".format(total_time, total))


The outcome of our script is six again, but now, our runtime is only three seconds (the highest sleep).

Source code in my github.

Topics:
big data ,python ,threads ,sleep ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}