What I have is electron app that runs the frontend and python app that runs in backend. At some point I need to execute python script and get info from it.
I'm currently using python shell for node.js (communicate through standard I/O), however I was able to find another method with sockets. What are the advantages of using sockets compared to standard I/O? From what I understand, I can only pass strings either way. Which performs better?
Related
I have a web server which I have developed an application on using php and SQL, mainly picked php as I am more comfortable with it.
In short the application automates some of our network tasks .
As part of this I have automated some solarwinds tasks and the library orionsdk doesnt have a php library so I have used python.
It's all working fine but I really need to run these python scripts from my browser .
I have considered using php shell exec and got my python scripts to accept args so I can run them and parse the output.
I know I could also use flask or django but worry I will have a flask app to maintain aswell as a php app.
I think the question is what the best way to achieve this or any way which I haven't mentioned .
Any help would be very much appreciated
So you want PHP to communicate with Python and you've already mentioned using shell commands and http traffic.
I can imagine you could also achieve something similar by connecting up both PHP and Python up to the same database. In that case PHP could write a record in a table and Python could pick that up and do something with the data in there. Python could be either be a long-running process or fired off by a cronjob in this case. If a database seems overkill you could also write a file to some place on disk, which Python can pick up.
Personally I'd go for the shell exec approach if you want to keep it light weight and for a API connection if you want to have a more robust solution which needs to be expanded later on.
I have a running CLI application in Python that uses threads to execute some workers. Now I am writing a GUI using electron for this application. For simple requests/responses I am using gRPC to communicate between the Python application and the GUI.
I am, however, struggling to find a proper publishing mechanism to push data to the GUI: gRPCs integrated streaming won't work since it uses generators; as already mentioned my longer, blocking tasks are executed using threads (subclasses of threading.Thread). Also I'd like to emit certain events (e.g., the progress) from within those threads.
Then I've found the Flasks SocketIO implementation, which is, however, a blocking execution and thus not really suited for what I have in mind - I'd have to again execute two processes (Flask and my CLI application)...
Another package I've found is websockets but I can't get my head around how I could implement this producer() function that they mention in the patterns.
My last idea would be to deploy a broker-based message system like Redis or simply fall back to the brokerless zmq, which is a bit of a hassle to setup for the GUI application.
So the simple question:
Is there any easy framework that allows to create a server-"task" in a Python that I can pass messages to publish to?
For anyone struggling with concurrency in python:
No, there isn't any simple framework. IMHO pythons' concurrency handling is a bit of a mess (compared to other languages like golang, where concurrency is built in). There's multiple major packages implementing this, one of them asyncio, but most of them are incompatible. I've ended up using a similar solution like proposed in this question.
I'm not sure what I'm trying to do is possible, but I'm trying to write a NodeJS application that needs to call some Python functions, the way I'm going to use Python functions is similar to How to call Python function from NodeJS by using child_process
If the python script had a few imports that had a slight delay when running the script, if you were calling the python script often surely it would cause problems for the applications running time. Is there a way to get around to constantly keep a child process python call open and then call a function whenever it's needed?
Thanks
There's nothing specific to Python nor node.js here. If you don't want the overhead of spawning a child process, make the "other" script / application / whatever a long running process and use any kind of inter-process communication.
I need to get data (json) in my html page, with help of Ajax. I have a Nodejs server serving requests.
I have to get the json from server, which is python code to process and produce json as output.
So should i save json in db and access it? (seems complicated just for one single use)
Should i run python server, to serve the requests with json as result (call it directy from html via ajax)
Should i serve requests with nodejs alone, by calling python method from nodejs? if so how to call the python method.
If calling python requires it to run server ? which one to prefer (zeropc, or some kind of web framework?)
Which is the best solution? or Which is preferred over other in what scenario and what factors?
As I understand:
You have a server running
an app that acts as a webserver, made with nodejs.
another app in python that does not expose an HTTP api, but you would like to interact with it in some way?
You have control over the source code of both
When running big systems with parts programmed on different languages their are many ways to make it all work together.
The choice is yours, and you give us little information to help you decide which way is better. It all depends on how big are your python and nodejs apps, and what they do.
There are no magical way to call a python method from nodejs, or vice versa
Spawn persistent processes, and make them communicate with sockets, or pipes. Generally this is done with enabling libraries (like ZeroMQ + a serialization/RPC format).
Spawn persistent processes and make them communicate with a message queue in between (RabbitMQ, ActiveMQ, ...)
Spawn your nodejs webserver, and use the "child_process" module to spawn a process in python, and interact with it throught its standard input/output. There are helper libraries to do this: https://www.npmjs.com/package/python-shell
Just spawn the nodejs webserver and execute python scripts with "child_process" when you need them (no persistent python process).
Rewrite it all in python
Rewrite it all in nodejs
In your application, if you have some requirement of processing results of python server requests in your nodejs application, then you need to call the python server requests in nodejs app with request libray and then process the result. Otherwise, you should simply call python server resources through client side ajax requests.
Thanks
Hi I want to deploy a matlab application on the web using python. Is there a way to do it.I have converted my application into jar files (java classes) as per the documentation on math works site. Can someone point me in the right direction to go ahead
The fact that your Matlab code is packaged up as Jars may not help that much here, at least not with pure Python.
There are a few ways you can take code written in Java and expose it to Python.
Jython
If you are willing to give Jython a shot this may be a really easy way to provide a Django interface to your jars.
Basically you'll get to write a normal Django App and also use Jython to work natively with your Jars. This could be the best of both worlds assuming you aren't tied to CPython.
Django-Jython
Java Compatibility Interfaces
On CPYTHON either of the following projects will help you work with the code in your Jar files:
JCC: Create a Python extension module that wraps your Jar file
JPype: Provides an API for running the JVM and calling into code running in that JVM from Python.
Separate Process:
If you have a standalone program written in Matlab (really any language) you could execute it as a child process of your Django application. You'd look into a simple web form in Django that allowed you to submit values to be inputs to this process and then in your view (after validating the form) you'd do something like:
command = "mymatlabprogram.exe %s"%(arg1,)
process = subprocess.Popen(command.split())
stdout, stderr = process.communicate()
Assuming that worked you could pull answers out of stdout or error messages out of stderr. You could serve an image created by that process, etc. Once something like this is working you could look into celeryd to extract the subprocess stuff from your web app.
The advantage of working with a separate process is that you isolate bugs in your Matlab code from breaking your web application and vice versus. The disadvantage is you have to serialize everything and work with multiple times between the client's browser and your web app, between the web app and the executable, and back to the client.