Slow performance while connecting to remote mariadb database from exe built using pyinstaller on windows 10 as compared to directly from pycharm IDE

Viewed 266

I am facing slow performance while connecting to a remote mariadb database from .exe built using pyinstaller on windows 10 as compared to directly from pycharm IDE.

So, below is the sample code:

import MySQLdb
try:
    conn = MySQLdb.connect(host=database_host, port=database_port, user=database_user,
                           passwd=database_password, db=database_name)
except MySQLdb.OperationalError as e:
    # error message
    print('Error: ', e)

If I run this code from pycharm, the connection time is ~100 milliseconds to a remote mariadb database.

If I run the same code using a windows executable (.exe built using pyinstaller) the execution time is approximately 2 seconds to the same remote mariadb database.

pyinstaller command:

pyinstaller -y  --add-data "C:\path\to\data" --icon="C:\path\to\icon.ico" --hidden-import babel.numbers package.path.MainModule.py

I installed maridb on the local windows machine and didn't find any performance difference. So, the performance difference is observed only for the remote connection.

This is is a significant difference in the performance. Now, I can't figure out what could be causing this. Any direction will be greatly appreciated.

1 Answers

Alright, after spending some more time and trying to find difference between the two execution environments, just realised a silly mistake, that I made, was causing the problem.

I was using host name while executing the using .exe where as I was using direct IP address while executing form the pycharm.

So, it was time taken to resolve the hostname.

Here is how I found it: Executed the same program from a python venv form windows command line. The test result was same as pycharm.

Now, I printed the values of database_host and database_port. The only difference found between the .exe and pycharm (or the command line) is the the value of database_host name was hostname in case of .exe and ip address in the pycharm.

So, I changed the configuration while running the .exe to use the IP address, the performance same now.

Update: I applied workaround now by adding mapping of IP address and the hostname in the C:\widows32\drivers\etc\hosts file. Works like a charm

Related