# pip3 install sseclient-py json5 requests
import sseclient
import requests
import concurrent.futures
import json5
def task1(event):
# task code here
print("[+] Got Event: ", event.event)
print(json5.loads(event.data))
def task2(event):
# task code here
print("[+] Got Event: ", event.event)
print(json5.loads(event.data))
def task3(event):
# task code here
print("[+] Got Event: ", event.event)
print(json5.loads(event.data))
my_api = "https://example.com/lol"
response = requests.get(my_api, stream=True)
if response.status_code == 200:
executor = concurrent.futures.ThreadPoolExecutor()
client = sseclient.SSEClient(response)
# Loop forever (while connection "open")
for event in client.events():
if event.event == "Task1":
executor.submit(self.task1, event)
elif event.event == "Task2":
executor.submit(self.task2, event)
elif event.event == "Task3":
executor.submit(self.task3, event)
Above is the example, it is working absolutely fine, the problem I'm facing is that, while checking the event.event to if-else ladder,
it is taking some fraction of seconds, and this time delay is eventually increasing, after 2-3 minutes, I'm facing time delay of around 6-11 seconds, which is problematic in my case.
I'm using concurrent.futures module so that while processing the event data, it will not block the for loop, but still it is not that helpful
What should I do, so that time delay is minimum around 1 sec max
Thanks in advance