0

views.py:

from threading import Thread

class PoliceJobs:
    def call_police_defence_jobs(request):
        job         = PoliceDefenceJobs.police_jobs(request)
        sleep(0.5)
        job_details = PoliceDefenceJobDetails.police_defence_job_details(request)
        message = call_all(job,job_details)
        return HttpResponse(message)

    def call_statewise_police_jobs(request):
        job         = PoliceDefenceJobs.statewise_police_jobs(request)
        sleep(0.5)
        job_details = PoliceDefenceJobDetails.statewise_police_job_details(request)
        message = call_all(job,job_details)
        return HttpResponse(message)

def police_jobs(request):
    try:
        t1 = Thread(target=PoliceJobs.call_police_defence_jobs,args=[request])
        t2 = Thread(target=PoliceJobs.call_statewise_police_jobs,args=[request])
        t1.start()
        t2.start()
        t1.join()
        t2.join()
        return HttpResponse("success")
    except:
        return HttpResponse("error")

urls.py

from django.urls import path
from .views import police_jobs

urlpatterns = [
    path('finish_police_jobs/', police_jobs),
]

error in shell:

Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/home/soubhagya/Desktop/carrier-circle/backend/finalize/views.py", line 840, in call_police_defence_jobs
    job         = PoliceDefenceJobs.police_jobs(request)
AttributeError: type object 'PoliceDefenceJobs' has no attribute 'police_jobs'

in PoliceJobs class i changed the function name to PoliceDefenceJobs.police_jobs which is not exist to make error.

Here i am making error and handling it by adding except block but it is still showing error in console but not in browser. in browser it is showing success wheather there is an exception.

Tej
  • 86
  • 5

1 Answers1

0

Exceptions in threads don't propagate to the thread that created them. See Catch a thread's exception in the caller thread in Python for a workaround.

Arman Ordookhani
  • 6,031
  • 28
  • 41