Start cherryd in a subprocess.
(self, imports=None)
| 440 | f.write(str(conf)) |
| 441 | |
| 442 | def start(self, imports=None): |
| 443 | """Start cherryd in a subprocess.""" |
| 444 | portend.free(self.host, self.port, timeout=1) |
| 445 | |
| 446 | args = [ |
| 447 | '-m', |
| 448 | 'cherrypy', |
| 449 | '-c', self.config_file, |
| 450 | '-p', self.pid_file, |
| 451 | ] |
| 452 | r"""Command for running cherryd server with autoreload enabled. |
| 453 | |
| 454 | Using |
| 455 | |
| 456 | ``` |
| 457 | ['-c', |
| 458 | "__requires__ = 'CherryPy'; \ |
| 459 | import importlib.metadata, re, sys; \ |
| 460 | sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]); \ |
| 461 | sys.exit(\ |
| 462 | importlib.metadata.distribution('cherrypy').entry_points[0])"] |
| 463 | ``` |
| 464 | |
| 465 | doesn't work as it's impossible to reconstruct the `-c`'s contents. |
| 466 | Ref: https://github.com/cherrypy/cherrypy/issues/1545 |
| 467 | """ |
| 468 | |
| 469 | if not isinstance(imports, (list, tuple)): |
| 470 | imports = [imports] |
| 471 | for i in imports: |
| 472 | if i: |
| 473 | args.append('-i') |
| 474 | args.append(i) |
| 475 | |
| 476 | if self.daemonize: |
| 477 | args.append('-d') |
| 478 | |
| 479 | env = os.environ.copy() |
| 480 | # Make sure we import the cherrypy package in which this module is |
| 481 | # defined. |
| 482 | grandparentdir = os.path.abspath(os.path.join(thisdir, '..', '..')) |
| 483 | if env.get('PYTHONPATH', ''): |
| 484 | env['PYTHONPATH'] = os.pathsep.join( |
| 485 | (grandparentdir, env['PYTHONPATH'])) |
| 486 | else: |
| 487 | env['PYTHONPATH'] = grandparentdir |
| 488 | self._proc = subprocess.Popen([sys.executable] + args, env=env) |
| 489 | if self.wait: |
| 490 | self.exit_code = self._proc.wait() |
| 491 | else: |
| 492 | portend.occupied(self.host, self.port, timeout=5) |
| 493 | |
| 494 | # Give the engine a wee bit more time to finish STARTING |
| 495 | if self.daemonize: |
| 496 | time.sleep(2) |
| 497 | else: |
| 498 | time.sleep(1) |
| 499 |