I wrote a python program which uses a module (pytesseract, specifically) and I notice it takes a few seconds to import the module once I run it. I am wondering if there is a way to initialise the module before running the main program in order to cut the duration of the actual program by a few seconds. Any suggestions?
Asked
Active
Viewed 79 times
0
-
I think you should explain *why* your program needs to start right away and the few seconds of loading modules is unacceptable for your use case. You would probably get better responses, since this could be an X-Y problem. – Reedinationer May 08 '19 at 17:18
-
What steps did you take to make sure that it is importing pytesseract that is causing the slowdown? – soundstripe May 08 '19 at 17:21
-
Nothing special, the terminal reads 'importing Pytesseract' for a good 4 seconds – Lorenzo Catani May 08 '19 at 17:30
2 Answers
0
One possible solution for slow startup time would be to split your program into two parts--one part that is always running as a daemon or service and another that communicates with it to process individual tasks.
As a quick answer without more info, pytesseract also imports (if they are installed) PIL, numpy, and pandas. If you don't need these, you could uninstall them to reduce load time.

soundstripe
- 1,454
- 11
- 19
-
1Interesting, I don't need pandas or numpy in fact. Could you expand on how I could process communications between programs (I'm on MacOs). Thanks! – Lorenzo Catani May 08 '19 at 17:28
-
the built-in multiprocessing library provides some capability in this area. https://stackoverflow.com/questions/6920858/interprocess-communication-in-python – soundstripe May 08 '19 at 19:06
0
I presume that you need to start your application multiple times with different arguments and you don't want to waste time on imports every time, right?
You can wrap actual code in while True:
and use input()
to get new arguments. Or read arguments from the file.

Alex Bodnya
- 120
- 10