Can I run a bot inside a Django project that will run independently of other Django apps?

150 Views Asked by At

I have a Django project for a website. And for the same website, I run another Python bot to scrap some data. I was wondering if I could somehow attach the bot to the Django project so that both can run on the same server without causing harm to each other. The bot and other Django apps should run independently of each other.

Does anyone know how this can be done?

Any help will be appreciated.

1

There are 1 best solutions below

1
resun On

with the multiprocessing module I got a solution. In the manage.py file of a Django project, there's an if statement at the end that just calls the main function defined in the same file.

I created two processes using the multiprocessing.Process class. One for the main function and one for the bot I wanted to attach to the project. Then I started those processes and the bot was running as well as other apps of the Django project.

here's the code:

import multiprocessing

# A function to start the bot

def start_bot():
    ...

if __name__ == '__main__':
    bot_process = multiprocessing.Process(name='bot_process', target=start_bot)
    main_process = multiprocessing.Process(name='main_process', target=main)
    bot_process.start()
    main_process.start()

This is just a way of solving the problem. If you have a better solution please add an answer.