Reputation: 5239
Assuming you are processing a live stream of data like this:
What would be the best way to have a background Thread
to update the data
variable while the main logic can do_some_logic
within the endless loop?
I have some experience with clear start and end point of parallelization using multiprocessing/multithreading
, but I am unsure how to continously execute a background Thread updating an internal variable. Any advice would be helpfull - Thanks!
Upvotes: 1
Views: 1752
Reputation: 23743
Have the background thread make separate DataFrames with data retrieved from the live feed that can be sent to the main thread and appended to a DataFrame in the main thread. The DataFrames should have the same structure.
Upvotes: 1
Reputation: 7343
Write an update function and periodically run a background thread.
def update_data(data):
pass
import threading
def my_inline_function(some_args):
# do some stuff
t = threading.Thread(target=update_data, args=some_args)
t.start()
# continue doing stuff
Understand the constraints of GIL so you know if threading is really what you need.
I'd suggest you to look into async/await to get a better idea of how threading actually works. It's a similar model to javascript: your main-program is single-threaded and it exploits IO-bound tasks to context switch into different parts of your application.
If this doesn't meet your requirements, look into multiprocessing - specifically, how to spin a new process and how to share variables between processes
Upvotes: 3