I have written on_ticks to process tick data. But my processing takes time. Before it is over, new tick data comes in and a new on_ticks function with new tick data starts. How to handle this ? Any tips would be appreciated.
1. Initialize internal queue Q in the beginning of the program 2. Inside on_ticks you pass the data to the internal queue immediately 3. No need to launch threads inside on_ticks 4. You create thread in the main program for doing tasks 5. Code inside this thread reads from Q the internal queue and processes the data
def read_queue(): while True: bulk_ticks = messageQueue.get() # do something like NASA or SpaceX here with this data
# this is the only thing you should write in on_ticks, anything more would be terrible coding def on_ticks(ws , ticks): messageQueue.put(ticks)
# before starting KiteTicker, run read_queue in a thread so that it starts and gets hooked to the queue # below as an example, you can use new python 3 library also for threading import _thread _thread.start_new_thread(read_queue, ())
@samirkelekar @vaibhavsharma13
For beginners
1. Initialize internal queue Q in the beginning of the program
2. Inside on_ticks you pass the data to the internal queue immediately
3. No need to launch threads inside on_ticks
4. You create thread in the main program for doing tasks
5. Code inside this thread reads from Q the internal queue and processes the data
and there should be a separate thread that is running and consuming this queue of ticks to process
this way you have decoupled on_ticks from computational load
This is a very simple approach, you can use a proper message broker also instead of python queue
You can use celery, rq.