# Path: src/gwf/backends/local.py
# class Server:
#     def __init__(self, hostname="", port=0, num_workers=None):
#         self.hostname = hostname
#         self.port = port
#         self.num_workers = num_workers
# 
#         self.manager = Manager()
#         self.status = self.manager.dict()
#         self.queue = self.manager.Queue()
#         self.waiting = self.manager.dict()
#         self.lock = self.manager.Lock()
# 
#     def handle_request(self, request):
#         try:
#             logger.debug("Received request %r", request)
#             return request.handle(self.queue, self.status)
#         except Exception:
#             logger.error("Invalid request %r", request, exc_info=True)
# 
#     def handle_client(self, conn):
#         logger.debug("Accepted client connection.")
#         try:
#             while True:
#                 request = conn.recv()
#                 response = self.handle_request(request)
#                 if response is not None:
#                     conn.send(response)
#         except EOFError:
#             logger.debug("Client connection closed.")
# 
#     def wait_for_clients(self, serv):
#         while True:
#             client = serv.accept()
#             self.handle_client(client)
# 
#     def start(self):
#         """Starts a server that controls local workers.
# 
#         Calling this function starts a pool of `num_workers` workers used to run
#         targets sent to the server. The server will run indefinitely unless shut
#         down by the user.
#         """
#         try:
#             serv = Listener((self.hostname, self.port))
#             workers = Pool(
#                 processes=self.num_workers,
#                 initializer=Worker,
#                 initargs=(self.status, self.queue, self.waiting, self.lock),
#             )
# 
#             logging.info(
#                 "Started %s workers, listening on port %s",
#                 self.num_workers,
#                 serv.address[1],
#             )
#             self.wait_for_clients(serv)
#         except OSError as e:
#             if e.errno == 48:
#                 raise ServerError(
#                     (
#                         "Could not start workers listening on port {}. "
#                         "The port may already be in use."
#                     ).format(self.port)
#                 )
#         except KeyboardInterrupt:
#             logging.info("Shutting down...")
#             workers.close()
#             workers.join()
#             self.manager.shutdown()
# 
# Path: src/gwf/conf.py
# CONFIG_DEFAULTS = {"verbose": "info", "backend": "local", "check_updates": True}
# class FileConfig:
#     def __init__(self, path, data, defaults=None):
#     def validator(self, key):
#         def _inner(func):
#     def _validate_value(self, key, value):
#     def get(self, key, default=None):
#     def __getitem__(self, key):
#     def __setitem__(self, key, value):
#     def __delitem__(self, key):
#     def __len__(self):
#     def __iter__(self):
#     def dump(self):
#     def load(cls, path, defaults=None):
# def config_from_path(path):


import multiprocessing
import click
from ..backends.local import Server
from ..conf import config




@click.command()
@click.option(
    "-n",
    "--num-workers",
    type=int,

# Path: src/gwf/backends/local.py
# class Server:
#     def __init__(self, hostname="", port=0, num_workers=None):
#         self.hostname = hostname
#         self.port = port
#         self.num_workers = num_workers
# 
#         self.manager = Manager()
#         self.status = self.manager.dict()
#         self.queue = self.manager.Queue()
#         self.waiting = self.manager.dict()
#         self.lock = self.manager.Lock()
# 
#     def handle_request(self, request):
#         try:
#             logger.debug("Received request %r", request)
#             return request.handle(self.queue, self.status)
#         except Exception:
#             logger.error("Invalid request %r", request, exc_info=True)
# 
#     def handle_client(self, conn):
#         logger.debug("Accepted client connection.")
#         try:
#             while True:
#                 request = conn.recv()
#                 response = self.handle_request(request)
#                 if response is not None:
#                     conn.send(response)
#         except EOFError:
#             logger.debug("Client connection closed.")
# 
#     def wait_for_clients(self, serv):
#         while True:
#             client = serv.accept()
#             self.handle_client(client)
# 
#     def start(self):
#         """Starts a server that controls local workers.
# 
#         Calling this function starts a pool of `num_workers` workers used to run
#         targets sent to the server. The server will run indefinitely unless shut
#         down by the user.
#         """
#         try:
#             serv = Listener((self.hostname, self.port))
#             workers = Pool(
#                 processes=self.num_workers,
#                 initializer=Worker,
#                 initargs=(self.status, self.queue, self.waiting, self.lock),
#             )
# 
#             logging.info(
#                 "Started %s workers, listening on port %s",
#                 self.num_workers,
#                 serv.address[1],
#             )
#             self.wait_for_clients(serv)
#         except OSError as e:
#             if e.errno == 48:
#                 raise ServerError(
#                     (
#                         "Could not start workers listening on port {}. "
#                         "The port may already be in use."
#                     ).format(self.port)
#                 )
#         except KeyboardInterrupt:
#             logging.info("Shutting down...")
#             workers.close()
#             workers.join()
#             self.manager.shutdown()
# 
# Path: src/gwf/conf.py
# CONFIG_DEFAULTS = {"verbose": "info", "backend": "local", "check_updates": True}
# class FileConfig:
#     def __init__(self, path, data, defaults=None):
#     def validator(self, key):
#         def _inner(func):
#     def _validate_value(self, key, value):
#     def get(self, key, default=None):
#     def __getitem__(self, key):
#     def __setitem__(self, key, value):
#     def __delitem__(self, key):
#     def __len__(self):
#     def __iter__(self):
#     def dump(self):
#     def load(cls, path, defaults=None):
# def config_from_path(path):


# Path: src/gwf/plugins/workers.py
import multiprocessing
import click
from ..backends.local import Server
from ..conf import config





@click.command()
@click.option(
    "-n",
    "--num-workers",
    type=int,

    default=config.get("local.num_workers", multiprocessing.cpu_count()),