Skip to content

Hanging script at the end of Python multiprocessing

While writing and stress-testing a Python script which uses the multiprocessing library, I ran into the problem that occasionally the script hangs at the end. Literally runs past the last line of code and then hangs.

In this script I'm using Queues and Events, so I made sure that I properly close the queues in the forked/spawned (tried both) processes and also clean out all queues in the parent. Nevertheless occasionally - like seldom, but it happens - the script hangs. Checked the process list, checked remaining threads, checked the queues, all fine. Still ...

When I hit Ctrl+C, I get the following stack trace:

^CError in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python3.9/multiprocessing/", line 300, in _run_finalizers
  File "/usr/lib/python3.9/multiprocessing/", line 224, in __call__
    res = self._callback(*self._args, **self._kwargs)
  File "/usr/lib/python3.9/multiprocessing/", line 201, in _finalize_join
  File "/usr/lib/python3.9/", line 1053, in join
  File "/usr/lib/python3.9/", line 1073, in _wait_for_tstate_lock
    if lock.acquire(block, timeout):

(Can't change the Python 3.9, as I don't control the target system where this is supposed to run)

There's nothing left to join, the only two remaining threads are MainThread and QueueFeederThread.


After some more debugging I had the simple idea not only to empty the queue before finishing the script, but setting the queue variable to None:

worker_queue = None

This helps. The script is no longer hanging at the end. At a later point I will see if the problem is fixed in newer versions.

Django: Remove default entries from admin menu

The Django administration site comes with a couple of default entries, depending on which apps and middleware is installed.

Usually there is at least "Users" and "Groups", and if the quite common "allauth" is installed, then there is also "Site", "SocialApp", "SocialAccount", and "SocialToken". They are not always necessary, especially when no Social Login is used. Or why have the "Site" administration when only Site=1 is used?

With a few tricks these can be removed from the admin menu.


Continue reading "Django: Remove default entries from admin menu"

Django: disable inline option to add new referenced objects

The Django Web Framework makes it quite easy to add new referenced objects in the admin menu.

Let's say the model has two foreign keys in it:

class TeamMember(models.Model):
    team = models.ForeignKey(Team, on_delete=models.CASCADE, verbose_name=_("Team"))
    user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, verbose_name=_("User"))

And the admin form:

class TeamMemberForm(forms.ModelForm):
    class Meta:
        model = TeamMember

class CustomTeamMemberAdmin(admin.ModelAdmin):
    form = TeamMemberForm
    add_form = TeamMemberForm
    model = TeamMember
    fieldsets = [
        (None,      {'fields': ['team', 'user']}),
    ], CustomTeamMemberAdmin)

Then Django will show small a small green "Django add reference" sign next to the fields:

Continue reading "Django: disable inline option to add new referenced objects"

Use namespace as global variable in Ansible Jinja templates

A simple task, or so I thought: in a Jinja template keep track of the number of items in a loop. And then use that count afterwards.

Disclaimer: the number of items is not equal the number of times the loop runs, so I can't use the loop variables.

Turns out that Jinja has other opinions, and variables inside a loop are all local. When a variable is changed inside the loop, the scoop of the variable stays local, and once the loop ends the original value of the variable is restored. That's even true for variables which are created outside the loop.


Continue reading "Use namespace as global variable in Ansible Jinja templates"

Certificate expiration date in Ansible

In one of my Ansible Playbooks I'm updating Let's Encrypt certificates. Because the Playbook is rather long, I want to make sure that Ansible is not spending cycles on a certificate which is not about to expire. Hence I wrote a small filter plugin, which receives the certificate path and the number of days the certificate must be still valid.

This plugin is used to filter out any "good" certificate which does not need to be updated.


Continue reading "Certificate expiration date in Ansible"