Celery – schedule tasks with pauses between them

I needed to schedule 10 tasks that have these conditions

  1. A time gap between tasks is fixed (but varies)
  2. No need to pass results of one task to another
  3. Need to read a database in every task (important: at the moment of the execution)

I tried a few options, and this one worked

 ret = chain(task1.si(args).set(countdown=30*1), ..., task10.si(args).set(countdown=30*10)).apply_sync()

Important note – the signature here is not .s() but .si() this way you don’t pass the result of task1 to task2. You set the countdown with set() and not inside the apply_sync().

It is simple but it took me some time to figure out.

How to add GIF to a static background (PIL + imageio) V.2 – The Better One

My previous version has flaws. It works well for opaque GIFs, but OpenCV is famous for not being good with transparent images. So instead of OpenCV I use imageio for GIF iteration and PIL for adding GIF frames to the static background.

Why do more?

The original algorithm left these artifacts, look at the cyan block on the intersection of GIF frame and a darker abstract part:

The artefact in question
Read More »

Video Generation Journey. Part 1: Making frames (problems 1-4)

Initially, I aimed to make one big post, but I am splitting it up. This Part 1 is about how I made the frames to generate videos from.

Intro

I have been on and off doing this project for a few months. Many things did not work for me – from ffmpeg to sheer OpenCV/ffmpeg installation on Apple M1 systems – and I am not 100% sure what made it work to this day. I plan to put myself in a situation where I have to do it again, and I will make a guide then. However, right now, I can only say that this video tutorial on running native ffmpeg and this guide from OpenCV themselves are good places to start.

The last time I used OpenCV was in 2017 when I worked on my graduate work. Back then, I processed prerecorded videos with no need to worry about audio. So I had some background prior to this project, but I was in no way ready for the ride.

Buckle up!

Table of contents

  • What do I have to create?
  • Limitations
    • Background
    • Phrases
    • Audio
  • Problems
    • Problem 1: GIF on a background
    • Problem 2: Putting text on the image
    • Problem 3: Text wrapping
    • Problem 4: Text blocks positioning
  • Results so far
Read More »

[Note] ‘ChatConsumer’ has no attribute ‘as_asgi’ – Sloppy development

Following Channels’ docs I encountered yet another error the solution to what was not obvious right off the bat.

Ditch Channels.

The End.

This was my original post in October 2020 and I stand by it.

You can make your project work with Google Firebase - it handles signals well enough. Yes it is primarily used by mobile developers, but overall it is fine for web too.

I spent trying to install Django Channels on my local Linux 18.04, and I kind of made it work, but then I had to deploy it... And as a professional backender you don't want to let people down.

I decided to try Channels because my Python-related friends talked about it. But days into the trouble I asked my friend about a certain step and he asked me in return "oh so [you use it because] they fixed Channels since two years ago?". I as like ??????????????????. So that moment I ditched it. Too much time wasted, but I like the result I achieved with Firebase.

What I am saying - Firebase is fast and universal. In a matter of days I managed to make and deploy an internal Customer Support chat service that connected mobile app users and a support from the web.</p>

I will be writing a new series on Django + Firebase, and this post is pretty much a short introduction.

ffmpeg: Non-monotonous DTS in output stream 0:0 – Video Generation Series (Part 2) [ERROR]

This error occured when I used ffmpeg to concatenate different videos that I generated:

ffmpeg -f concat -safe 0 -i list.txt -c copy final_video.avi -y
BTW, if you want to run ffmpeg or any other command inside a python script (a very reasonable idea), use subprocess:

command = "ffmpeg -f concat -safe 0 -i list.txt -c copy final_video.avi -y"
subprocess.Popen(command.split(), stdout=subprocess.PIPE)

As a result – the videos got smashed together – some frames even never appeared.

Read More »

How to access Model’s pk/id when you create an instance in Django

If you need to modify instance fields depending on the instance’s pk or id, hop on.

When you create an instance, before it is saved, you can’t have pk or id as it is not yet created.

    def save(self, *args, **kwargs):
        # self.pk == None
        # self.id == None
        super().save(*args, **kwargs)
        # self.pk != None
        # self.id != None        

If you have a property that somehow depends on the value of self.pk or self.id and you want to have it saved on creating, you have two options.

Read More »