8

I have a python script. Lets say http://domain.com/hello.py, which only prints "Hello, World!".

Is it possible to precompile this Python file?

I get around 300 requests per second and the overhead of compiling is way to high. In Java the server can handle this easily but for calculations Python works much easier.

3
  • 2
    Python compiles .py files to .pyc on import, meaning it's only done once. What makes you think it's the "overhead of compiling" that's making your app slow? Commented May 9, 2015 at 0:12
  • @univerio, I don't import anything I just have one line "print 'hello world!" I tried testing the load with Jmeter and it could handle only about 2000 requests per minute. My Java servlet which does db queries calculations etc. can handle about 70k per minute. What else could be the cause of this? Commented May 9, 2015 at 0:16
  • 2
    this is more likely an artifact of you trying to serve python files as normal cgi .... try at least fcgi... but really you should be using nginx + gnuicorn or some kind of caching ... apache+mod_wsgi also would work... the problem with serving them as normal cgi (and maybe fcgi... Im not sure) is that its having to spool up a completely new interpreter environment for each request Commented May 9, 2015 at 0:20

3 Answers 3

11

the problem is not that you need to "precompile" python, the problem is that you are trying to execute python scripts using normal cgi script stuff...

the real answer is to use a better web backend than simple cgi to run your python

I would suggest the following in order of appearance

  1. nginx + gnunicorn
  2. apache2 + mod-wsgi
  3. something else
  4. anything else
  ...
n-1. fcgi
  n. cgi

I know this isnt really an answer and is entirely opinion based

Sign up to request clarification or add additional context in comments.

Comments

2

Python code is compiled automatically the first time it is run, by the CPython (standard Python) interpreter. You can pre-compile it if you want to optimize the first request, but that usually isn't necessary. Aside from that, you'd need to convert your Python code into a Python C / cython Module. There are some tools to help you convert Python code into a Python Module if that's the route you want to go.

There's also a Python module called SciPy that's commonly used for Scientific Computing and Data Science applications, which provides a tool called Weave which allows you to inline C/C++ code into your Python code allowing certain performance-critical portions of the code to run using compiled C/C++ code.

1 Comment

I dont think this really addresses OP problem (which is probably mis-stated in the title and OP ...) "precompiling" python is not the actual solution that will help the OP ... and i dont think you can really optimize print "hello world" with any amount of scipy or numpy
1

via the python interface where your python source file is abc.py:

  1. import py_compile
  2. py_compile.compile('abc.py')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.