<commit_before>#!/usr/bin/env python
"""TaskProcess class to provide helper methods around worker task's process.

Attributes:
    log (logging.Logger): the log object for this module

"""

import asyncio
import logging
import os
import signal
from asyncio.subprocess import Process

log = logging.getLogger(__name__)


class TaskProcess:
    """Wraps worker task's process."""

    def __init__(self, process: Process):
        """Constructor.

        Args:
            process (Process): task process
        """
        self.process = process
        self.stopped_due_to_worker_shutdown = False

    async def worker_shutdown_stop(self):
        """Invoke on worker shutdown to stop task process."""
        self.stopped_due_to_worker_shutdown = True
        await self.stop()

    async def stop(self):
        """Stop the current task process.

        Starts with SIGTERM, gives the process 1 second to terminate, then kills it
        """
        # negate pid so that signals apply to process group
        pgid = -self.process.pid
        try:
            os.kill(pgid, signal.SIGTERM)
            await asyncio.sleep(1)
            os.kill(pgid, signal.SIGKILL)
        except (OSError, ProcessLookupError):
            return<commit_msg>Add type annotation to worker_shutdown_stop, which was called in a typed context<commit_after>#!/usr/bin/env python
"""TaskProcess class to provide helper methods around worker task's process.

Attributes:
    log (logging.Logger): the log object for this module

"""

import asyncio
import logging
import os
import signal
from asyncio.subprocess import Process

log = logging.getLogger(__name__)


class TaskProcess:
    """Wraps worker task's process."""

    def __init__(self, process: Process):
        """Constructor.

        Args:
            process (Process): task process
        """
        self.process = process
        self.stopped_due_to_worker_shutdown = False

    async def worker_shutdown_stop(self) -> None:
        """Invoke on worker shutdown to stop task process."""
        self.stopped_due_to_worker_shutdown = True
        await self.stop()

    async def stop(self):
        """Stop the current task process.

        Starts with SIGTERM, gives the process 1 second to terminate, then kills it
        """
        # negate pid so that signals apply to process group
        pgid = -self.process.pid
        try:
            os.kill(pgid, signal.SIGTERM)
            await asyncio.sleep(1)
            os.kill(pgid, signal.SIGKILL)
        except (OSError, ProcessLookupError):
            return
{'fraction_non_alphanumeric': 0.08284714119019837, 'fraction_numerical': 0.0015558148580318942, 'mean_word_length': 2.547586206896552, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6624573', 'n_tokens_mistral': 708, 'n_tokens_neox': 684, 'n_words': 237}