Skip to content

stop#

It is possible to stop the worker from running by using the stop method. The supervisor receives the request, and will never spawn a new worker instead. A worker uses this method when there is no solution to a problem. Using this method in conjunction with on_starvation is a good example of how it should be used. If a worker is starving and there are not enough tasks available to consume, it can be intentionally stopped to reduce the load on the queue. By calling stop, all the workers under a specific supervisor will stop as well.

When the worker is interrupted by a SIGTERM, the signal handler invokes the stop function to interrupt the worker's workflow.

Definition#

1
2
3
def stop(
    self,
) -> None

Examples#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def work(
    self,
    task,
):
    url_to_crawl = task.kwargs['url']

    response = requests.get(url_to_crawl)
    try:
        self.database.insert_crawl_result(
            url=url_to_crawl,
            content=response.content,
        )
    except pymongo.errors.ServerSelectionTimeoutError:
        self.stop()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def work(
    self,
    task,
):
    url_to_crawl = task.kwargs['url']

    response = requests.get(url_to_crawl)
    self.database.insert_crawl_result(
        url=url_to_crawl,
        content=response.content,
    )

def on_failure(
    self,
    task,
    exception,
):
    if isinstance(exception, pymongo.errors.ServerSelectionTimeoutError):
        self.stop()

def on_stop(
    self,
    task,
):
    self.database.close()