3

Looking to use a include a redis server for storing application specific data with my pyinstaller bundled application.
Before getting into it hands-on, need some guidance.

Are these the steps to follow for it?
(1) Bundle redis-server executable. And run it as a standalone application via some script in my bundled package.
(2) Use redis client packages in python to connect to the redis-server

I guess (2) should surely work. But is there any easy way of doing (1).

Vishal
  • 3,178
  • 2
  • 34
  • 47

1 Answers1

0

You can bundle arbitrary binaries with the --add-binary option on the command line or the binaries argument to the Analysis call in your .spec file. Check out the manual for details, but one example:

pyinstaller -F main.py --add-binary=`which redis-server`:bin

I don't know of a way to run arbitrary executables, but you could have some python code in your app to detect when you're bundled, find the redis binary, and start it up. Again, you can check out the documentation for details on how to go about this but, again, a example of how this could look (optional contextmanager elegance stolen from another answer):

import sys
import os
import subprocess
from contextlib import contextmanager


@contextmanager
def bundledredis():
    proc = subprocess.Popen(
        [os.path.join(sys._MEIPASS, 'bin', 'redis-server')])
    yield
    proc.terminate()


@contextmanager
def optional(condition, context_manager):
    if condition:
        with context_manager:
            yield
    else:
        yield


def realmain():
    print('doing stuff')


def main():
    with optional(getattr(sys, 'frozen', False), bundledredis()):
        realmain()


if __name__ == '__main__':
    main()
Turn
  • 6,656
  • 32
  • 41