def worker_main(conn): from importlib import import_module common_data = None while True: try: job = pickle_loads(eintr_retry_call(conn.recv_bytes)) except EOFError: break except KeyboardInterrupt: break except Exception: prints('recv() failed in worker, terminating worker', file=sys.stderr) import traceback traceback.print_exc() return 1 if job is None: break if not isinstance(job, Job): if isinstance(job, File): with lopen(job.name, 'rb') as f: common_data = f.read() common_data = pickle_loads(common_data) else: common_data = job continue try: if '\n' in job.module: import_module('calibre.customize.ui') # Load plugins from calibre.utils.ipc.simple_worker import compile_code mod = compile_code(job.module) func = mod[job.func] else: func = getattr(import_module(job.module), job.func) if common_data is not None: job.kwargs['common_data'] = common_data result = func(*job.args, **job.kwargs) result = Result(result, None, None) except Exception as err: import traceback result = Result(None, as_unicode(err), traceback.format_exc()) try: eintr_retry_call(conn.send_bytes, pickle_dumps(result)) except EOFError: break except Exception: prints('send() failed in worker, terminating worker', file=sys.stderr) import traceback traceback.print_exc() return 1 return 0