2023-02-26 05:49:39 +00:00
|
|
|
from logging import getLogger
|
2023-03-06 03:37:39 +00:00
|
|
|
from os import getpid
|
2023-03-07 14:02:53 +00:00
|
|
|
from queue import Empty
|
2023-02-28 04:52:43 +00:00
|
|
|
from sys import exit
|
2023-02-26 20:15:30 +00:00
|
|
|
|
2023-02-26 19:09:24 +00:00
|
|
|
from setproctitle import setproctitle
|
2023-02-26 05:49:39 +00:00
|
|
|
|
2023-02-26 18:32:48 +00:00
|
|
|
from ..server import ServerContext, apply_patches
|
2023-02-26 21:21:58 +00:00
|
|
|
from ..torch_before_ort import get_available_providers
|
2023-02-26 20:15:30 +00:00
|
|
|
from .context import WorkerContext
|
2023-02-26 05:49:39 +00:00
|
|
|
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
2023-03-06 03:37:39 +00:00
|
|
|
EXIT_ERROR = 1
|
|
|
|
EXIT_INTERRUPT = 0
|
|
|
|
EXIT_MEMORY = 2
|
|
|
|
EXIT_REPLACED = 3
|
|
|
|
EXIT_SUCCESS = 0
|
|
|
|
|
2023-02-26 18:32:48 +00:00
|
|
|
|
2023-02-27 23:14:53 +00:00
|
|
|
def worker_main(context: WorkerContext, server: ServerContext):
|
2023-02-26 18:32:48 +00:00
|
|
|
apply_patches(server)
|
2023-02-26 20:15:30 +00:00
|
|
|
setproctitle("onnx-web worker: %s" % (context.device.device))
|
2023-02-26 18:32:48 +00:00
|
|
|
|
2023-03-20 01:16:52 +00:00
|
|
|
logger.trace(
|
|
|
|
"checking in from worker with providers: %s", get_available_providers()
|
|
|
|
)
|
2023-02-26 21:06:40 +00:00
|
|
|
|
2023-03-06 02:07:06 +00:00
|
|
|
# make leaking workers easier to recycle
|
|
|
|
context.progress.cancel_join_thread()
|
|
|
|
|
2023-02-26 05:49:39 +00:00
|
|
|
while True:
|
2023-02-27 02:09:42 +00:00
|
|
|
try:
|
2023-03-18 20:32:49 +00:00
|
|
|
if not context.is_active():
|
2023-03-07 14:02:53 +00:00
|
|
|
logger.warning(
|
|
|
|
"worker %s has been replaced by %s, exiting",
|
|
|
|
getpid(),
|
2023-03-18 20:32:49 +00:00
|
|
|
context.get_active(),
|
2023-03-07 14:02:53 +00:00
|
|
|
)
|
2023-03-06 03:37:39 +00:00
|
|
|
exit(EXIT_REPLACED)
|
2023-03-06 03:28:21 +00:00
|
|
|
|
2023-03-19 22:57:14 +00:00
|
|
|
# wait briefly for the next job
|
2023-03-18 20:12:09 +00:00
|
|
|
job = context.pending.get(timeout=1.0)
|
2023-03-19 22:57:14 +00:00
|
|
|
logger.info("worker %s got job: %s", context.device.device, job.name)
|
2023-02-28 04:45:29 +00:00
|
|
|
|
2023-03-19 22:57:14 +00:00
|
|
|
# clear flags and save the job name
|
|
|
|
context.start(job.name)
|
2023-03-18 20:12:09 +00:00
|
|
|
logger.info("starting job: %s", job.name)
|
2023-03-19 22:57:14 +00:00
|
|
|
|
|
|
|
# reset progress, which does a final check for cancellation
|
2023-03-18 20:12:09 +00:00
|
|
|
context.set_progress(0)
|
|
|
|
job.fn(context, *job.args, **job.kwargs)
|
2023-03-19 22:57:14 +00:00
|
|
|
|
|
|
|
# confirm completion of the job
|
2023-03-18 20:12:09 +00:00
|
|
|
logger.info("job succeeded: %s", job.name)
|
2023-03-19 22:57:14 +00:00
|
|
|
context.finish()
|
2023-02-28 04:37:43 +00:00
|
|
|
except Empty:
|
|
|
|
pass
|
2023-02-28 04:52:43 +00:00
|
|
|
except KeyboardInterrupt:
|
2023-02-28 05:12:53 +00:00
|
|
|
logger.info("worker got keyboard interrupt")
|
2023-03-19 22:57:14 +00:00
|
|
|
context.fail()
|
2023-03-06 03:37:39 +00:00
|
|
|
exit(EXIT_INTERRUPT)
|
2023-03-18 20:16:41 +00:00
|
|
|
except ValueError:
|
|
|
|
logger.exception("value error in worker, exiting: %s")
|
2023-03-19 22:57:14 +00:00
|
|
|
context.fail()
|
2023-03-06 03:37:39 +00:00
|
|
|
exit(EXIT_ERROR)
|
2023-02-26 17:16:33 +00:00
|
|
|
except Exception as e:
|
2023-03-16 23:34:28 +00:00
|
|
|
e_str = str(e)
|
|
|
|
if "Failed to allocate memory" in e_str or "out of memory" in e_str:
|
2023-03-06 03:07:06 +00:00
|
|
|
logger.error("detected out-of-memory error, exiting: %s", e)
|
2023-03-19 22:57:14 +00:00
|
|
|
context.fail()
|
2023-03-06 03:37:39 +00:00
|
|
|
exit(EXIT_MEMORY)
|
2023-03-06 03:07:06 +00:00
|
|
|
else:
|
2023-03-17 03:29:07 +00:00
|
|
|
logger.exception(
|
|
|
|
"error while running job",
|
2023-03-06 03:07:06 +00:00
|
|
|
)
|
2023-03-19 22:57:14 +00:00
|
|
|
context.fail()
|
2023-03-18 20:12:09 +00:00
|
|
|
# carry on
|