Bradley Kirton's Blog

Published on June 2, 2023

Go home

A script for running Django runserver and the TailwindCSS watch commands

The following script assumes your application uses source layout.

#!/usr/bin/env python

import asyncio
import functools
import signal
import logging
import sys

logging.basicConfig(level=logging.INFO, stream=sys.stdout)
logger = logging.getLogger(__name__)

def process_shutdown(signum: int, loop: asyncio.AbstractEventLoop, event: asyncio.Event) -> None:
    """Set the shutdown event.

    :param signum: The signal received.
    :param loop: The running event loop instance.
    :param event: The shutdown event to set.
    """"Received {signum} signal, will shutdown soon...")

async def amain() -> None:
    """Run the django runserver and tailwind watch processes."""

    loop = asyncio.get_running_loop()
    shutdown_event = asyncio.Event()"Setting up signal handlers...")

    for signum in (signal.SIGINT, signal.SIGTERM):
            functools.partial(process_shutdown, signum=signum, loop=loop, event=shutdown_event),
        )"Spawning subprocesses...")

    processes = await asyncio.gather(
        asyncio.subprocess.create_subprocess_shell("python src/server/ runserver localhost:8000"),
        asyncio.subprocess.create_subprocess_shell("tailwindcss -w -i src/server/static/css/_tailwind.css -o src/server/static/css/tailwind.css"),

    await shutdown_event.wait()"Sending SIGTERM to processes...")

    for process in processes:
        process.send_signal(signal.SIGTERM)"Waiting for processes to shutdown...")

    for process in processes:
        await process.wait()

if __name__ == "__main__":