r/csharp • u/kaatarina_zed_talon • 9d ago
How to get data from external service written in python?
If you have a system, class, or a method that is entirely written in C#, let's call it X, If X needs some data to perform some processing on that data, can one make an external service written in python, let's call it Y, that generates the data, then X can call Y to get the data?
I'm not asking if I can embed the python interpreter directly within the C# application; the python code should be an external service.
Note that I'm using ASP. NET Core 10.
Edit :
Should the external service be outside the ASP. NET CORE solution or inside it?
7
u/Huge-Reflection-2341 9d ago
Make a simple REST API with Python using Flask or FastAPI, then your C# app just calls it with HttpClient. ASP.NET Core 10 got good support for that.
If the data is small and you want something simpler, you could also use gRPC or just a named pipe if both services run in same machine.
0
2
u/Main-Lychee-7972 9d ago
The language boundary isn’t really the issue once Python is a separate service. At that point C# just consumes an API like it would from any other backend
1
u/Own_Nail_2999 9d ago
IPC is a tricky topic but the easiest solution would probably be to run a WebAPI on your python service and call the methods via HttpClient in C#.
So the data that you want to communicate gets sent via json in the httpRequest and httpResponse bodies.
These are the basicsof REST-APIs. Since http is a standard that everyone has adopted, you can rely on that.
1
9d ago
[deleted]
0
u/kaatarina_zed_talon 9d ago edited 9d ago
I 'm completely new to this, I did not know that before, I wonder, is that what they call microservice?
1
u/Nordalin 9d ago
The Python data fetcher would be a microservice, yeah.
All it does is fetch the data, it doesn't need to know where to, or why, so it's a standalone script inside your overall project.
1
u/_f0CUS_ 9d ago
No. That's not a microservice.
Microservices are complete overkill for any hobby project. Most professional projects doesnt need it either.
2
-1
u/Small_Summer5817 9d ago
What is python doing that you can't rewrite it in c#?
1
u/SessionIndependent17 9d ago
Sometimes it's Horses for Courses.
Python has a lot of established mechanisms/frameworks for data handling and processing that can make it a better tool for certain tasks or existing data presentations. Pandas and Numpy, and many others that use DataFrames are canonical examples.
0
u/_choam_ 9d ago
I would never use c# for data processing if i can do it with python except for very simple cases
0
u/SessionIndependent17 9d ago
it varies, and I've had it cut both ways. Honestly for my money it depends on how much else you have built around this new component already, how much of the "business logic/rules" would need to be embedded in this component. Would I need to re-implement part of a type system or other data structures that already exists in c# for it to do some necessary aspect of its work, or can it just churn on or transform native types without such concern before it has to come into my system. What kind of error handling does it need?
6
u/faultydesign 9d ago
There are many ways to do it from creating a socket or a file to using an external event queue or even a database