Python vs C++ for CUDA web server? - python

I am planning on writing some software for a web server that uses machine learning to process large amounts of data. This will be real estate data from a MySQL server. I will be using the CUDA framework from Nvidia with python/caffe or the c++ library. I will be using a Tesla P100. Although python is more widely used for machine learning I presume it is hard to write a server app in python without sacrificing performance. Is this true? Is c++ well supported for machine learning? Will anything be sacrificed by writing a professional server app in python (ex: connecting to MySQL database)?

Python is a language that performs worse than c++ in terms of runtime for several reasons:
First and foremost, Python is a scripting language that runs with an interpreter as opposed to c++ which compiled into machine code before running.
Secondly: python runs in the background a garbage collector system while in c++ the memory management is done manually by the programmer.
In your case, I recommend that you work with Python for several reasons:
Writing in Python in CUDA allows you to compile the code even though it is Python (CUDA provides JIT - Just In Time compiler, as well as a compiler and other effective tools), Which greatly improves performance
Python provides many, rich varied libraries that will help you a lot in the project, especially in the field of machine learning.
The development time and code length will be significantly shorter in Python.
From my experience with working at CUDA in the Python language I recommend you use numba and numbapro libraries, they are comfortable to work with and provide support for many libraries like numpy.
Best of luck.

Both C++ and Python are perfectly reasonable languages for implementing web servers. Python is actually quite common, and there are many frameworks for writing web servers in Python such as flask, bottle, django, etc. Architecturally, I wonder whether you really need the machine learning (which I imagine would be a data processing pipeline) and the web server to be the same process / binary; however, even if you do them in the same server, I suspect that either language would be perfectly reasonable; moreover, if you ever came to the point where you needed to run a piece of computation in C++ for performance, using SWIG to call C++ from Python or using some form of message passing from Python to a C++ helper process (such as via gRPC) are options.

Related

Diagnosing memory leak from Windows DLL accessed in Python with ctypes

I've written an abstraction layer in Python for a piece of commercial software that has an API used for accessing the database back end. The API is exposed via a Windows DLL, and my library is written in Python.
My Python package loads the necessary libraries provided by the application, initializes them, and creates a couple of Python APIs on top. There are low level functions that simply wrap the API, and make the functions callable from Python, as well as a higher level interface that makes interaction with the native API more efficient.
The problem I'm encountering is that when running a daemon that uses the library, it seems there is a memory leak. (Several hundred KB/s) I've used several Python memory profiling tools, as well as tested each function individually, and only one function seems to leak, yet no tool reports that memory has been lost during execution of that function.
On Linux, I would use Valgrind to figure out if the vendor's library was the culprit, but the application only runs on Windows. How can I diagnose whether the vendor is at fault, or if it's the way I'm accessing their library?
I ended up writing a program in C without dynamic memory allocation to test the library. The leak is indeed in one of the functions I'm calling, not the Python program.

Which Python version should I use with Raspberry Pi running web applications?

I'm totally new in Python world.
I want to create a web application with some Python code behind. I want to use Python to control Raspberry Pi inputs and outputs etc.
There are Python 2 and Python 3 available. I've read some about these version, but I'm still not sure which version I should use.
Unless you have a specific reason to stick with Python 2 (e.g. a framework that has not been ported yet), you should really be using Python 3.
Quoting Python wiki, "Python 2.x is legacy, Python 3.x is the present and future of the language".
Most of the books on the topic of Python and Raspberry Pi refer to Python 3.x. I'm finding a lot of online courses and books are focusing more on 3.x than 2.7. Unless you're working at a company that's on Python 2.x and don't plan on going to 3.x, you're better off learning Python 3.x.
It depends, what web framework you are going to use.
Some of them might have a bit limited functionality on Python 3 but still can be worth to use.
This could be case of Flask, which is very lightweight, provides all what you need, but according to heavy users lack in few small details complete support for Python 3. This situation is likely to be resolved in near future, but if you want to have it developed now, it is better to use the version of Python, which fits your web framework.
Comments on few (not all) web frameworks
Django
Very popular, but will force you to do things in Django style.
Final solution can become a bit heavier, then really necessary, this could be a problem on Raspberry Pi, which has very limited resources available.
Flask
Also rather popular (even though not as much as Django).
Gives you freedom to use only what you need.
Very good tutorials.
Most of the applications run under Python 2 and Python 3, few (supporting) libraries are told to be not ported completely yet (I cannot serve exactly which ones).
CherryPy
Minimalistic web framework, but with very good builtin HTTP and WSGI server.
Not so easy to find good tutorials, best is using (now a bit old) book about programming in CherryPy.
Note: By default, the applications are developed in debug mode and code is autoreloaded from disk. This disk activity can slow down on RPi and consume some energy, so if you have troubles with that, set the app to production mode.
Conclusions
My current choice is using Flask on Python 2.7, but this is partially due to a lot of legacy code I have developed in Python 2.7.
You shall make your decision about what framework you are going to use and check, what is status of Python 3 support.

Can PiCloud hold FORTRAN code? (website hosted by GAE)

I have a some old Fortran77 codes, which does some calculations. Now I build a website hosted by Google App Engine, and need to call those models' calculation results. Since I am new to both GAE and PiCloud, my basic questions are:
Should I first compile those Fortran77 code using a windows compiler?
Then, publish those models to PiCloud
Call from GAE?
Does my approach make sense? or Does PiCloud has Fortran77 environment, which can directly do the calculation without compiling first? If so, is there any example about this topic?
Thanks!
Picloud claim "install any library or binary written in any language", so it's safe to say you can run fortran programs on it. In fact, their homepage even says:
You can deploy any software written in any programming language
including C, C++, Java, R, Fortran, Ruby, etc.
You shouldn't compile it with a windows compiler, because picloud runs linux - compile it using a linux compiler, such as GCC.
Regarding using it from App Engine, see this page, where it says:
[...] use PiCloud from Google AppEngine, since our cloud client library is not supported on GAE.

Hosting a non-trivial python program on the web?

I'm a complete novice in this area, so please excuse my ignorance.
I have three questions:
What's the best (fastest, easiest, headache-free) way of hosting a python program online?
I'm currently looking at Google App Engine and Web Frameworks for Python, but all the options are a bit overwhelming.
Which gui/viz libraries will transfer to a web app environment without problems?
I'm willing to sacrifice some performance for the sake of simplicity.
(Google App Engine can't do C libraries, so this is causing a dilemma.)
Where can I learn more about running a program locally vs. having a program continuously run on a server and taking requests from multiple users?
Currently I have a working Python program that only uses standard Python libraries. It currently uses around 2.7gb of ram, but as I increase my dataset, I'm predicting it will use closer to 6gb. I can run it on my personal machine, and everything is just peachy. I'd like to continue developing on the front end on my home machine and implement the web app later.
Here is a relevant, previous post of mine.
Depending on your knowledge with server administration, you should consider a dedicated server. I was doing running some custom Python modules with Numpy, Scipy, Pandas, etc. on some data on a shared server with Godaddy. One program I wrote took 120 seconds to complete. Recently we switched to a dedicated server and it now takes 2 seconds. The shared environment used CGI to run Python and I installed mod_python on the dedicated server.
Using a dedicated server allows COMPLETE control (including root access) to the server which allows the compilation and/or installation of anything. It is a bit pricy but if you're making money with your stuff it might be worth it.
Another option would be to use something like http://www.dyndns.com/ where you can host a domain on your own machine.
So with that said, perhaps some answers:
It depends on your requirements. ~4gb of RAM might require a dedicated server. What you are asking is not necessarily an easy task so don't be afraid to get your hands dirty.
Not sure what you mean here.
A server is just a computer that responds to requests. On the dedicated server (I keep mentioning) you are operating in a Unix (or Windows) environment just like you would locally. You use SOFTWARE (e.g. Apache web server) to serve client requests. My vote is mod_python.
It's a greater headache than a dedicated server, but it should be much closer to your needs to go with an Amazon EC2 instance.
http://aws.amazon.com/ec2/#instance
Their extra large instance should be more than large enough for what you need to do, and you only turn the instance on when you need it so you don't have the massive bill that you get with a dedicated server that's the same size.
There are some nice javascript based visualization toolkits out there, so you can model your application to return raw (json) data and render that on the client.
I can mention d3.js http://mbostock.github.com/d3/ and the JavaScript InfoVis Toolkit http://thejit.org/

python _+ django, is it compiled code?

Just looking into python from a .net background.
Is python compiled like .net?
If yes, can it be obfuscated and is it more or less secure than .net compiled code that is obfuscated?
does pretty much every web host (unix) support django and python?
There are many implementations of the Python language; the three that are certainly solid, mature and complete enough for production use are CPython, IronPython, and Jython. All of them are typically compiled to some form of bytecode, also known as intermediate code. The compilation from source to bytecode may take place as and when needed, but you can also do it in advance if you prefer; however Google App Engine, which lets you run small Python web apps, including Django, for free, as one of its limitations requires you to upload source and not compiled code (I know of no other host imposing the same limitation, but then I know of none giving you so many resources for free in exchange;-).
You might be most at home with IronPython, which is a Microsoft product (albeit, I believe, the first Microsoft product to be entirely open-source): in that case you can be certain that it is "compiled like .net", because it is (part of) .net (more precisely, .net and silverlight). Therefore it cannot be neither more nor less obfuscated and/or secure than .net (meaning, any other .net language).
Jython works on JVM, the Java Virtual Machine, much like IronPython works on Microsoft's Common Language Runtime, aka CLR. CPython has its own dedicated virtual machine.
For completeness, other implementations (not yet recommended for production use) include pypy, a highly flexible implementation that supports many possible back-ends (including but not limited to .net); unladen swallow, focused on evolving CPython to make it faster; pynie, a Python compiler to the Parrot virtual machine; wpython, a reimplementation based on "wordcode" instead of "bytecode"; and no doubt many, many others.
CPython, IronPython, Jython and pypy can all run Django (other implementations might also be complete enough for that, but I'm not certain).
I don't know about the security part but.
Python is interpreted. Like PHP. (It's turned into bytecode which CPython reads)
Django is just a framework on top of Python.
Python can be compiled.
And no not all hosts support python + django.
You shouldn't have to worry about obfuscating your code, specially since it's going to run on your server.
You are not supposed to put your code in a public directory anyway. The right thing to do with django (as oposed to PHP) is to make the code accessible by the webserver, but not by the public.
And if your server's security has been breached, then you have other things to worry about...
Obfuscation is false security. And the only thing worse than no security is false security. Why would you obfuscate a web app anyways?
Python is compiled to bytecode and run on a virtual machine, but usually distributed as source code.
Unless you really plan to run your webapp on "pretty much every web host" that question doesn't matter. There are many good hosts that support python and django.
Code obfuscation in .NET are mostly a question of changing variable names to make it harder to understand the disassembled code. Yes, you can do those techniques with CPython too.
Now, why ever you would want to is another question completely. It doesn't actually provide you with any security, and does not prevent anybody from stealing your software.
The Python is interpreted language. But you can compile the python program into a Unix executable using Freeze.

Categories