'Api: Django with Api-Rest and Scrapy [ValueError: signal only works in main thread of the main interpreter]

I'm trying to return a dictionary with an Api, that dictionary is being filled with a Scrapy script

from rest_framework.views import APIView
from django.shortcuts import HttpResponse

from scrapy.crawler import CrawlerProcess
from scrapyProject.spiders.spider import ProductoSpider, outputResponse

class Prueba(APIView):
def post(self, request):
    dato = request.data['dato']
    if dato == 'dato':
        datos = []
        process = CrawlerProcess({
            'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
        })

        process.crawl(ProductoSpider)
        process.start()
        datos = outputResponse
        return HttpResponse(datos)  
    else:
        return HttpResponse("Adios", content_type='application/json')  

This is just an experiment. "outputResponse" is a global variable in my spider, and I've already checked out that the info is saving correctly with this:

from scrapy.crawler import CrawlerProcess
from spiders.spider import ProductoSpider, outputResponse

def execute():
   process = CrawlerProcess({
       'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
   })

   process.crawl(ProductoSpider)
   process.start()

   return outputResponse


print (execute())

This script does work, but when I want to try the Api doing "POST" in postman I get this:

Internal Server Error: /api/experimento
Traceback (most recent call last):
  File "C:\Users\Duvan Requena\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Duvan Requena\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Duvan Requena\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\decorators\csrf.py", 
line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\Duvan Requena\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Duvan Requena\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\Duvan Requena\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\Duvan Requena\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "C:\Users\Duvan Requena\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "D:\Sistemas\Django\quaesitor\quaesitorApi\api.py", line 22, in post
    process = CrawlerProcess({
  File "C:\Users\Duvan Requena\AppData\Local\Programs\Python\Python39\lib\site-packages\scrapy\crawler.py", line 281, in __init__
    install_shutdown_handlers(self._signal_shutdown)
  File "C:\Users\Duvan Requena\AppData\Local\Programs\Python\Python39\lib\site-packages\scrapy\utils\ossignal.py", line 19, in install_shutdown_handlers
    reactor._handleSignals()
  File "C:\Users\Duvan Requena\AppData\Local\Programs\Python\Python39\lib\site-packages\twisted\internet\posixbase.py", line 341, in _handleSignals
    _SignalReactorMixin._handleSignals(self)
  File "C:\Users\Duvan Requena\AppData\Local\Programs\Python\Python39\lib\site-packages\twisted\internet\base.py", line 1281, in _handleSignals
    signal.signal(signal.SIGINT, reactorBaseSelf.sigInt)
  File "C:\Users\Duvan Requena\AppData\Local\Programs\Python\Python39\lib\signal.py", line 47, in signal
    handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
ValueError: signal only works in main thread of the main interpreter
[04/Feb/2022 13:52:23] "POST /api/experimento HTTP/1.1" 500 110399

=====================

Postman



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source