Hi Streamlit community,
I recently made an app that displays a lot of images in two columns. Now the performance is not as fast as expected because of two things:
-lack of multithreading
-cache not working fully
The lack of multithreading makes the initial loading of the images quite slow. I tried to solve it with a concurrent.futures.ThreadPoolExecutor but when I call a method with it, it fails to display anything but does display the following error in the terminal:
For the caching it works only partially because it does not load every image again, but it does rebuild the UI elements such as the sliders etc. which makes the app unresponsive for a long time.
Attached here is a small code example demonstrating what I am trying to do:
Is there a way to couple the add_report_ctx to a threadPoolExecutor or is this only possible when manually creating threads?
On the second part of your answer regarding the rebuilding of UI elements, is there any example code for this? I don’t realy understand how I would create a dynamic UI if the UI is not build in any function call.
I’ve had success looping through the ThreadPoolExecutor's _threads attribute to add the context to all its threads right after submitting a task, so something like this:
executor = ThreadPoolExecutor()
for param in params:
executor.submit(thread_function, params)
for thread in executor._threads:
add_report_ctx(thread)
Potentially running add_report_ctx() multiple times on the same thread isn’t ideal conceptually, but it appears to simply add an attribute to the thread object, so it shouldn’t cause anything to break, at least.
With Python 3.7 and up I’ve used the pretty janky workaround of putting a short sleep in the initializer function that the ThreadPoolExecutor() takes – That allows the add_report_ctx() call to be made before the thread does any “real” work.
Not a great solution, of course, but the initializer function isn’t called with the spawned thread as one of its arguments, so we can’t put the add_report_ctx() call directly in there, unfortunately.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.