Skip to content

Instantly share code, notes, and snippets.

@epsi95
Created November 6, 2020 18:08
Show Gist options
  • Select an option

  • Save epsi95/072a89acfa8d92c2160a70f881ef80ed to your computer and use it in GitHub Desktop.

Select an option

Save epsi95/072a89acfa8d92c2160a70f881ef80ed to your computer and use it in GitHub Desktop.
import logging
import datetime
import random
import time
import threading
import requests
logging.basicConfig(format='%(process)d >> %(asctime)s - %(message)s', level=logging.INFO)
def send_speed(car_name="car_0"):
logging.info(f"{car_name} started sending data")
for i in range(5):
current_speed = random.randint(0, 100)
current_timestamp = datetime.datetime.now().timestamp()
data = {
"id": car_name,
"timestamp": round(current_timestamp),
"speed": current_speed
}
logging.info(f"{car_name} sending data: {data}")
s = requests.post("http://localhost:9000/data", json=data)
if s.status_code == 200:
logging.info(f"{car_name} data sent!")
else:
logging.info(f"{car_name} unable to send data!")
time.sleep(1)
logging.info(f"{car_name} finished sending data")
logging.info('This is an info message')
threads = list()
for index in ["car_0", "car_1", "car_2", "car_3", "car_4"]:
x = threading.Thread(target=send_speed, args=(index,))
threads.append(x)
x.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment