How to do error handling in fastapi - with multiple processes `queue`

39 views Asked by At

I just got into fastapi and would like to have error handling handled with multiple processing:

   if queue.qsize() == 1:
        fun()
    else:
        pass

def fun(nb_worker=2):
    global queue

    processes = [Process(target=runfrd.mp_worker, args=(queue,)) for _ in range(nb_worker)]
    for process in processes:
          process.start()

runfrd.mp_worker is

def mp_worker(queue, error_queue):
    while queue.qsize() > 0:
        record = queue.get()
        cls = MainClass(**record)
        cls.preloop()
        print(f'{record}----done')

I have multiple error types handled in MainClass, i.e. TypeError, and Customised Error, I would like to show the error in HTTP as

except TypeError:
    raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid input for claim_id: claim_id is expected to be a list of strings.")

I kind of get it working without queue, but it's not performing as expected with it. No matter what error it triggered, fun() always performs as 200OK

Any suggestion is appreciated.

0

There are 0 answers