0

Requirement:

Invoke Python script (on server) via C#, e.g. via webapi. Here restful endpoint receives parameter (e.g. via GET request) and results from Python execution is returned (e.g. as JSON).

I am aware of this:

How do I run a Python script from C#?

I have used this approach in the past. For data exchange I used:

  1. C# writes data to file system (e.g. from GET request's query strings)
  2. C# invokes Python, which picks up data from file system and writes results to file system
  3. C# reads data from file system and returns (e.g. as JSON)

Just wondering are there neater ways to integrate 'pure' Python these days?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
cs0815
  • 16,751
  • 45
  • 136
  • 299
  • 1
    What are you trying to do in the first place? You could use IronPython. You don't need to store data to the file system though, you can pipe the data to the script by writing to Process.StandardInput and read results from StandardOutput. Which, btw will be as slow as CGI – Panagiotis Kanavos Oct 03 '18 at 10:41
  • 1
    Or you could create a Jupyter notebook instead of a script and run it as a service. Or you could use SQL Server 2017's python support, if the script is related to data stored in the database. This is kind of a broad question – Panagiotis Kanavos Oct 03 '18 at 10:44
  • Thanks @PanagiotisKanavos. Did not know about pipe option. IronPython would not work as sci-kit learn is not supported/ported. Just curious could I invoke a pickled model via IronPython. – cs0815 Oct 03 '18 at 10:47
  • @PanagiotisKanavos the sql server route is an interesting possibility but we currently use sql server 2012 but will switch the 2017 in the near (?) future. Is the notebook option scalable-ish (e.g. let us say the restful endpoint to execute model given some input data is hit every few seconds). – cs0815 Oct 03 '18 at 10:50
  • That's why I ask what do you want to do. Is this a one-off or infrequent call? Or a frequent prediction request? SQL Server 2017 can not only use Python scripts, it can compile prediction models created from them and use them in queries. Jupyter as a service is great, unless you have high traffic - it's not a web server after all – Panagiotis Kanavos Oct 03 '18 at 10:51
  • 1
    Define scal-ish. Depending on the business it could be thousands or millions of requests per day. Cloud providers have started offering Jupyter notebooks as a service, eg [Azure Notebooks](https://notebooks.azure.com/). That, or a local Jupyter service may be enough to start. At some point though you may want to create a proper REST service. Or move to your preferred cloud provider's data science infrastructure – Panagiotis Kanavos Oct 03 '18 at 10:55
  • What about using sockets to communicate between the already-running python script and the C# application? A library such as zeroMQ makes this sort of thing quite easy. http://zeromq.org/ – John Go-Soco Oct 03 '18 at 11:14
  • @PanagiotisKanavos - I guess my question was regarding proper REST service. I guess one could always expose Python (model) using Python restful framework ... If I would use webapi would you use the pipe option mentioned above? Thanks! – cs0815 Oct 03 '18 at 11:22
  • @PanagiotisKanavos - did you refer to JupyterHub in the above? If so, it only works in Unix/Linux ... – cs0815 Oct 03 '18 at 13:14

0 Answers0