Create Multiprocess Function using Python Django
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source. (djangoproject.com)
Python Django version 1.7.9 > already supports multiprocessing features, this feature is useful when we need functions that can run in parallel and asynchronously.
Get Yudo Maryanto’s stories in your inbox
Join Medium for free to get updates from this writer.
Before we start make sure you have the Django Framework installed or you can clone the project from GitHub. After you clone, follow the next process by reading the readme.MD file
git clone https://github.com/app-generator/django-adminlte.gitAlso, don't forget to create a Virtual Environment and activate it.
python -m venv venv
source venv/bin/activateAdd the code below inside views.py.
#views.py
from multiprocessing import Process
def multiprocessing(self, name):
p = Process(target=self.process1, args=("process1"))
p2 = Process(target=self.process2, args=('process2'))
p3 = Process(target=self.process3, args=('process3'))
p.start()
p2.start()
p3.start()
print('process4')
def process1(name):
print(name)
def process2(self, name):
print(name)
def process3(self, name):
print(name)Process1, process2, and process3 functions will run in parallel and asynchronously. If we run the code process4 may appear first compared to process1, process2, and process3. Processes that run in multiprocessing will be executed randomly, it allows the data that appears to be displayed randomly.
If you have any other additions to this post you can leave a comment below and if you like this post please hit clap 👏 and share.





