I'm trying to do db connection through multiprocessing for my app. I assume that an error is occurring from where I call my function. I've been struggled with multiprocessing in these few days. I found the codes from https://engineering.talentpair.com/django-multiprocessing-153dbcf51dab/
here my code is as follows
views.py
from src.conf.multiproces import MultiProcess
class GridSearchAPI(generics.GenericAPIView):
@transaction.atomic
def post(self, request):
with MultiProcess() as mp:
mp.map(request)
returnData = mp.results()
if returnData['result']:
return Response(returnData, status=status.HTTP_200_OK)
else:
returnData["message"] = "no data found"
return Response(result, status=status.HTTP_400_BAD_REQUEST)
multiprocess.py
from __future__ import print_function, absolute_import, unicode_literals
import time
try:
from Queue import Empty
except ImportError:
from queue import Empty
# from pathos import
from multiprocessing import Process,cpu_count, Manager
import logging
import traceback
from django import db
from django.conf import settings
from django.core.cache import caches
from elasticsearch_dsl.connections import connections
from app.viewSet import SelectAPI
loggly = logging.getLogger('loggly')
class Timer(object):
""" Simple class for timing code blocks
"""
def __init__(self):
self.start_time = time.time()
def done(self):
end_time = time.time()
return int(end_time - self.start_time)
def close_service_connections():
""" Close all connections before we spawn our processes
This function should only be used when writing multithreaded scripts where connections need to manually
opened and closed so that threads don't reuse the same connection
https://stackoverflow.com/questions/8242837/django-multiprocessing-and-database-connections
"""
# close db connections, they will be recreated automatically
db.connections.close_all()
# close ES connection, needs to be manually recreated
conn = connections._conns
print(connections)
if len(conn) > 0:
connections.remove_connection("default")
# close redis connections, will be recreated automatcially
for k in settings.CACHES.keys():
caches[k].close()
def recreate_service_connections():
""" All this happens automatically when django starts up, this function should only be used when writing
multithreaded scripts where connections need to manually opened and closed so that threads don't reuse
the same connection
"""
# ES is the only one that needs to be recreated explicitly
connections.create_connection(hosts=[settings.DATABASES["default"]["HOST"]], timeout=20)
def threadwrapper(some_function, catch_exceptions=True):
""" This wrapper should only be used when a function is being called in a multiprocessing context
"""
def wrapper(queue, items):
recreate_service_connections()
# for i in items:
try:
rv = some_function(items)
except Exception:
rv = None
if catch_exceptions:
loggly.error("threadwrapper caught an error, continuing - %s" % traceback.format_exc())
else:
raise
queue.put(rv, block=False)
close_service_connections()
return wrapper
class MultiProcess(object):
""" Nicely abstracts away some of the challenges when doing multiprocessing with Django
Unfortunately, falls over when running tests so its not really tested
We implement this as a context manager so we dont have to worry about garbage collection calling __del__
"""
queue = None
item_count = 1
workers = []
def __init__(self, num_workers=None, max_workers=None, debug_print=False, status_interval=20):
if num_workers is None:
# always use at least one threads and leave one cpu available for other stuff
# but 1 is the minumum
self.num_workers = cpu_count() - 1
if self.num_workers < 2:
self.num_workers = 1
if max_workers and self.num_workers > max_workers:
self.num_workers = max_workers
else:
self.num_workers = num_workers
self.debug_print = debug_print
self.status_interval = status_interval
if debug_print:
print("Using %s workers" % self.num_workers)
def __enter__(self):
close_service_connections()
return self
def map(self, params):
try:
self.queue = Manager().Queue()
p = Process(target=SelectForm, args=(params,))
p.start()
# p.join()
self.workers.append(p)
self._wait()
except Exception as e:
print(e)
def _wait(self):
""" Wait for all workers to finish and wakes up peridocially to print out how much work has happened
"""
total_time = Timer()
while [p for p in self.workers if p.is_alive()]:
tpt = Timer()
for p in self.workers:
p.join(timeout=self.status_interval)
interval_secs = tpt.done() // 1000
# if we've timed out on the status interval, print it out and reset the counter
if self.debug_print and interval_secs >= self.status_interval:
tpt = Timer()
total_secs = total_time.done() // 1000
percent = (self.queue.qsize() * 100) // self.item_count
print("--------- {}% done ({}s elapsed) ---------".format(percent, total_secs))
def results(self):
rv = []
try:
while True:
rv.append(self.queue.get(block=False))
except Empty:
return rv
def __exit__(self, type, value, traceback):
# recreate the connections so we can do more stuff
recreate_service_connections()
and when i run this program, I get this error below
cannot pickle '_io.BufferedReader' object
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python\Python38-32\lib\multiprocessing\spawn.py", line 109, in spawn_main
fd = msvcrt.open_osfhandle(new_handle, os.O_RDONLY)
OSError: [Errno 9] Bad file descriptor