Skip to content

running with gunicorn gives error: "pkg_resources.DistributionNotFound: The 'zope.event' distribution was not found and is required by the application" #33

@Phaqui

Description

@Phaqui

When running korp-backend with gunicorn, as shown in the reproduction Containerfile, the application crashes on startup
with the error message pkg_resources.DistributionNotFound: The 'zope.event' distribution was not found and is required by the application.

After some troubleshooting, I found a fix that worked for me: Update the gevent dependency. Why does that fix the problem? Honestly, I'm not entirely sure.. But something to do with how packages are found and loaded, maybe older packages use a way of loading that uses
methods that some other dependencies is not still doing... I really don't understand this fully.

The commented-out line where the gevent dependency is upgraded is the fix, as the comment also says. The python version in debian bookwork is 3.11. Note that not all of the apt packages are required to run a minimal version of korp, some are just included because I have them in my "real" Containerfile.

Containerfile

FROM docker.io/library/debian:bookworm AS builder

RUN set -eux && \
    apt-get update && \
    apt-get install --no-install-recommends -y \
        git build-essential \
        python3 python3-venv python3-pip python3-dev \
        default-libmysqlclient-dev libglib2.0-0 libpcre3

# Install Corpus WorkBench (CWB) from locally downloaded .deb file
COPY cwb_3.5.0-1_amd64.deb /cwb/
RUN dpkg -i /cwb/cwb_3.5.0-1_amd64.deb

WORKDIR /korp

RUN set -eux && \
    git clone --depth 1 https://github.com/spraakbanken/korp-backend.git

RUN python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# THIS IS THE FIX FOR
# "pkg_resources.DistributionNotFound: The 'zope.event' distribution was not found and is required by the application"
#RUN sed --in-place 's/gevent.*/gevent~=25.5/' /korp/korp-backend/requirements.txt

RUN pip install --disable-pip-version-check --requirement /korp/korp-backend/requirements.txt
RUN pip install gunicorn



FROM docker.io/library/debian:bookworm

RUN set -eux; \
    apt-get update; \
    apt-get install --no-install-recommends -y python3 libmariadb3 libglib2.0-0 libpcre3

# the created virtual environment, with all requirements installed
COPY --from=builder /opt/venv /opt/venv

# the code repository, as cloned from git
COPY --from=builder /korp/korp-backend /korp/korp-backend

# the CWB binaries and libraries
COPY --from=builder /usr/bin/cqp /usr/bin/cqp
COPY --from=builder /usr/bin/cwb-scan-corpus /usr/bin/cwb-scan-corpus
COPY --from=builder /usr/lib/libcl.so /usr/lib/libcl.so

# This essentially activates the virutal environment
ENV PATH="/opt/venv/bin:$PATH"

WORKDIR /korp/korp-backend
CMD ["gunicorn", "--worker-class", "gevent", "--bind", "0.0.0.0:1234", "--workers", "4", "--max-requests", "250", "--limit-request-line", "0", "run:create_app()" ]

For completeness, here is a simple shell script to build the image, and run a container.

tasks.sh

#!/bin/bash

usage() {
    echo "usage: $0 <build|run>" >/dev/stderr
    exit
}

build_image() {
    podman build -t dbg-korp-backend -f Containerfile
}

run_image() {
    lang=$2
    configpy=/home/anders/projects/korp/gtweb2_config/config.py
    korpconfig="/home/anders/projects/korp/gtweb2_config/corpus_configs/$lang"
    cwbfiles=/home/anders/misc/cwb/corpora/gt_cwb/registry/

    podman run --rm --replace \
        --name dbg-korp-backend-$lang \
        -p 1234:1234 \
        -v $configpy:/korp/korp-backend/instance/config.py \
        -v $korpconfig:/corpora/corpus_config \
        -v $cwbfiles:/corpora \
        dbg-korp-backend
}

case "$1" in
    build)
        build_image
        ;;
    run)
        run_image
        ;;
    *) usage
        ;;
esac

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions