Skip to content

Instantly share code, notes, and snippets.

@paulwababu
Created March 30, 2022 09:37
Show Gist options
  • Save paulwababu/000da6c81c86c0bb3f2d948f53e43558 to your computer and use it in GitHub Desktop.
Save paulwababu/000da6c81c86c0bb3f2d948f53e43558 to your computer and use it in GitHub Desktop.
Threaded vs Un-threaded functions for the SMS API
from time import sleep, perf_counter
import requests
def task1():
print("Started Unthreaded Requests")
send_sms = requests.get('{{api_url}}?number=254797584194&text=hello')
print(send_sms.status_code)
start_time = perf_counter()
task1()
task1()
task1()
task1()
task1()
task1()
task1()
task1()
task1()
task1()
end_time = perf_counter()
print(f'It took {end_time- start_time: 0.2f} second(s) to complete send 10 messages')
###########################################################################################################3
## Threaded Functions
from time import sleep, perf_counter
from threading import Thread
def task(id):
print("Started Threaded Requests")
send_sms = requests.get('{{api_url}}?number=254797584194&text=hello')
return send_sms.status_code
start_time = perf_counter()
# create and start 10 threads
threads = []
for n in range(1, 11):
t = Thread(target=task, args=(n,))
threads.append(t)
t.start()
# wait for the threads to complete
for t in threads:
t.join()
end_time = perf_counter()
print(f'It took {end_time- start_time: 0.2f} second(s) to send 10 messages.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment