Soroush Ghodsi
Soroush Ghodsi

Reputation: 380

Python - Run Code Simultaneously

I have an endpoint which get's some data then runs some code that takes about 30 seconds then gives back a response specific to the data. I need to be able to hit the endpoints multiple times with different data within 30 seconds but the code still needs to run and give back the correct data specific result.

Here's what I mean:

class Foo(Controller):
    def POST(self, **kwargs):
        [Run Code That Takes 30 Seconds]
        Return [Result That Changes Bassed off POST request Sent]

When I run this right now and I hit the endpoint more than once in 30 seconds the code just restarts with the new data and completely ignore the old data and it's results.

How can I allow the endpoint to be hit more than once in the seconds but still give back the corresponding result? Happy to answer any questions!

Upvotes: 0

Views: 119

Answers (1)

User2342342351
User2342342351

Reputation: 2174

You should first profile your script in order to see if your task is cpu or io bound.

If your task is io bound:

Have a look at the asyncio library python 3.

Or at the thread library python 2 + python 3.

If your task is cpu bound

If your task is cpu bound, you can't use threads because of the GIL.

You will have the choice to:

  • use the multiprocessing library: python2 + python3.

  • run an instance of python per cpu core, each one running a different task.

  • use a task queue like Celery.

Upvotes: 1

Related Questions