0

I have a python script which receives data from NodeJS, uses it to gather data and then sends this data back to the NodeJS server any time a request is made.

The problem is that a part of this python script loads a lot of data from an external source meaning it takes a while to run so its not ideal that it ends after every single request just to have to reload the data again when the next request is sent (this loading the external data process can take up to 8 seconds each time a request is made).

How can I have a Python script that runs as a background process when the server starts and does not close until the server closes while still being able to receive and send data?

I have tried Pyshell but it seems that it isn't meant for running a constant file that sends and receives data in real time.

Thanks a lot!

Max Kenney
  • 43
  • 8
  • If u are having the script in Django, then my be you can look out for django_rq. This will help you queue the process as an offline task. [Check this out!](https://github.com/rq/django-rq) – vvk24 Jul 08 '20 at 13:36

1 Answers1

2

I don't know how your current communication works but what I'd recommend is to run the Node and Python processes seperately and use python Flask library to receive data from Node then you can just return the gathered data. You can use any http request library in node (https://stackoverflow.com/a/6158966/7803014) to send requests to python. This way python process can stay alive independently. If you don't want to manually start the python process every time you can use child_process library in Node (https://nodejs.org/api/child_process.html).

  • I am unfamiliar with Flask, my current system works so that a request is made and then it awaits a response from Python before continuing. In your opinion is this achievable using Flask and Post requests? – Max Kenney Jul 08 '20 at 14:45
  • 1
    Yes it is achievable with Flask indeed – M. Burak DÖNMEZ Jul 08 '20 at 16:05