# Path: pyjob/exception.py
# class PyJobError(Exception):
#     pass
# 
# class PyJobTaskLockedError(PyJobError):
#     pass
# 
# Path: pyjob/local.py
# CPU_COUNT = multiprocessing.cpu_count()
# 
# class LocalTask(Task):
#     """Locally executable :obj:`~pyjob.task.Task`"""
# 
#     def __init__(self, *args, **kwargs):
#         """Instantiate a new :obj:`~pyjob.local.LocalTask`"""
#         super().__init__(*args, **kwargs)
# 
#         self.queue = multiprocessing.Queue()
#         self.kill_switch = multiprocessing.Event()
#         self.processes = []
#         self.chdir = kwargs.get("chdir", False)
#         self.permit_nonzero = kwargs.get("permit_nonzero", False)
#         self._killed = False
# 
#     @property
#     def nprocesses(self):
#         """Getter for the number of concurrent :obj:`~pyjob.local.LocalProcess`"""
#         return self._nprocesses
# 
#     @nprocesses.setter
#     def nprocesses(self, nprocesses):
#         """Setter for the number of concurrent :obj:`~pyjob.local.LocalProcess`"""
#         if nprocesses > CPU_COUNT:
#             logger.warning("More processes requested than available CPUs")
#         self._nprocesses = nprocesses
# 
#     @property
#     def info(self):
#         """:obj:`~pyjob.local.LocalTask` information"""
#         if any(proc.is_alive() for proc in self.processes):
#             return {"job_number": self.pid, "status": "Running"}
#         return {}
# 
#     def close(self):
#         """Close this :obj:`~pyjob.local.LocalTask` after completion"""
#         if self._killed:
#             return
#         for proc in self.processes:
#             proc.join()
#         self.kill()
# 
#     def kill(self):
#         """Immediately terminate the :obj:`~pyjob.local.LocalTask`"""
#         if self._killed:
#             return
#         if not self.kill_switch.is_set():
#             self.kill_switch.set()
#         # This is a requirement to avoid access to memory-inaccessible processes
#         # The queue gets flushed by triggering the kill_switch
#         for proc in self.processes:
#             proc.join()
#         for proc in self.processes:
#             proc.terminate()
#         logger.debug("Terminated task: %d", self.pid)
#         self._killed = True
# 
#     def _run(self):
#         """Method to initialise :obj:`~pyjob.local.LocalTask` execution"""
#         if self._killed:
#             return
#         for _ in range(self.nprocesses):
#             proc = LocalProcess(
#                 self.queue,
#                 self.kill_switch,
#                 directory=self.directory,
#                 chdir=self.chdir,
#                 permit_nonzero=self.permit_nonzero,
#             )
#             proc.start()
#             self.processes.append(proc)
#         for script in self.script:
#             self.queue.put(script)
#         for _ in self.processes:
#             self.queue.put(None)
#         self.queue.close()
#         self.pid = uuid.uuid1().int
#         time.sleep(0.1)


import os
import sys
import time
import pytest
from pyjob.exception import PyJobError, PyJobTaskLockedError
from pyjob.local import CPU_COUNT, LocalTask
            task.run()
            files.extend(task.script + task.log)

        innerf()
        pytest.helpers.unlink(files)

    @pytest.mark.skip(reason="Unstable test")
    def test_terminate_5(self):
        scripts = [pytest.helpers.get_py_script(i, 1000000) for i in range(10)]
        with LocalTask(scripts, processes=min(CPU_COUNT, 2)) as task:
            with pytest.raises(PyJobError):
                task.run()
                pytest.helpers.unlink(task.script[4:])
        pytest.helpers.unlink(task.script[:4] + task.log)

    def test_terminate_6(self):
        scripts = [pytest.helpers.get_py_script(i, 50000) for i in range(100)]
        with LocalTask(scripts, processes=CPU_COUNT) as task:
            task.run()
            time.sleep(1)
            task.kill()
        all_scripts_found = all(os.path.isfile(path) for path in task.script)
        some_logs_found = any(os.path.isfile(log) for log in task.log) and not all(
            os.path.isfile(log) for log in task.log
        )
        pytest.helpers.unlink(task.script + task.log)
        assert all_scripts_found and some_logs_found

    def test_terminate_7(self):
        scripts = [pytest.helpers.get_py_script(i, 10000) for i in range(10000)]

# Path: pyjob/exception.py
# class PyJobError(Exception):
#     pass
# 
# class PyJobTaskLockedError(PyJobError):
#     pass
# 
# Path: pyjob/local.py
# CPU_COUNT = multiprocessing.cpu_count()
# 
# class LocalTask(Task):
#     """Locally executable :obj:`~pyjob.task.Task`"""
# 
#     def __init__(self, *args, **kwargs):
#         """Instantiate a new :obj:`~pyjob.local.LocalTask`"""
#         super().__init__(*args, **kwargs)
# 
#         self.queue = multiprocessing.Queue()
#         self.kill_switch = multiprocessing.Event()
#         self.processes = []
#         self.chdir = kwargs.get("chdir", False)
#         self.permit_nonzero = kwargs.get("permit_nonzero", False)
#         self._killed = False
# 
#     @property
#     def nprocesses(self):
#         """Getter for the number of concurrent :obj:`~pyjob.local.LocalProcess`"""
#         return self._nprocesses
# 
#     @nprocesses.setter
#     def nprocesses(self, nprocesses):
#         """Setter for the number of concurrent :obj:`~pyjob.local.LocalProcess`"""
#         if nprocesses > CPU_COUNT:
#             logger.warning("More processes requested than available CPUs")
#         self._nprocesses = nprocesses
# 
#     @property
#     def info(self):
#         """:obj:`~pyjob.local.LocalTask` information"""
#         if any(proc.is_alive() for proc in self.processes):
#             return {"job_number": self.pid, "status": "Running"}
#         return {}
# 
#     def close(self):
#         """Close this :obj:`~pyjob.local.LocalTask` after completion"""
#         if self._killed:
#             return
#         for proc in self.processes:
#             proc.join()
#         self.kill()
# 
#     def kill(self):
#         """Immediately terminate the :obj:`~pyjob.local.LocalTask`"""
#         if self._killed:
#             return
#         if not self.kill_switch.is_set():
#             self.kill_switch.set()
#         # This is a requirement to avoid access to memory-inaccessible processes
#         # The queue gets flushed by triggering the kill_switch
#         for proc in self.processes:
#             proc.join()
#         for proc in self.processes:
#             proc.terminate()
#         logger.debug("Terminated task: %d", self.pid)
#         self._killed = True
# 
#     def _run(self):
#         """Method to initialise :obj:`~pyjob.local.LocalTask` execution"""
#         if self._killed:
#             return
#         for _ in range(self.nprocesses):
#             proc = LocalProcess(
#                 self.queue,
#                 self.kill_switch,
#                 directory=self.directory,
#                 chdir=self.chdir,
#                 permit_nonzero=self.permit_nonzero,
#             )
#             proc.start()
#             self.processes.append(proc)
#         for script in self.script:
#             self.queue.put(script)
#         for _ in self.processes:
#             self.queue.put(None)
#         self.queue.close()
#         self.pid = uuid.uuid1().int
#         time.sleep(0.1)


# Path: pyjob/tests/test_local.py
import os
import sys
import time
import pytest
from pyjob.exception import PyJobError, PyJobTaskLockedError
from pyjob.local import CPU_COUNT, LocalTask

            task.run()
            files.extend(task.script + task.log)

        innerf()
        pytest.helpers.unlink(files)

    @pytest.mark.skip(reason="Unstable test")
    def test_terminate_5(self):
        scripts = [pytest.helpers.get_py_script(i, 1000000) for i in range(10)]
        with LocalTask(scripts, processes=min(CPU_COUNT, 2)) as task:
            with pytest.raises(PyJobError):
                task.run()
                pytest.helpers.unlink(task.script[4:])
        pytest.helpers.unlink(task.script[:4] + task.log)

    def test_terminate_6(self):
        scripts = [pytest.helpers.get_py_script(i, 50000) for i in range(100)]
        with LocalTask(scripts, processes=CPU_COUNT) as task:
            task.run()
            time.sleep(1)
            task.kill()
        all_scripts_found = all(os.path.isfile(path) for path in task.script)
        some_logs_found = any(os.path.isfile(log) for log in task.log) and not all(
            os.path.isfile(log) for log in task.log
        )
        pytest.helpers.unlink(task.script + task.log)
        assert all_scripts_found and some_logs_found

    def test_terminate_7(self):
        scripts = [pytest.helpers.get_py_script(i, 10000) for i in range(10000)]

        with LocalTask(scripts, processes=min(CPU_COUNT, 2)) as task: