hero banner for blog post

How to create a round robin scheduling program in Python

6 min read

Introduction

This blog post covers how to implement Round Robin Scheduling in Python. We’ll look at what is required to get started with Nylas. We recently discussed this topic on our Coding with Nylas Livestream:

Let’s dive in!

What is round-robin scheduling?

Round Robin Scheduling is a method to handle multiple requests given limited resources. For example, when we share calendar availability across multiple service providers, a limited resource, allowing customers to book an appointment.

The Nylas Scheduler allows us to implement round robin scheduling where we select different scheduling algorithms programmatically. Applying efficient scheduling algorithms enables us to optimize for our specific use case.

When do you need a round robin scheduler?

When a user wants to book an appointment, you can configure different approaches to surfacing the available time slots. This is when round robin scheduling is useful. Using round robin scheduling allows you to modify displaying availability across different calendars. We will take a look at the different round robin configuration options shortly.

How Round Robin Scheduling Python Algorithm will work

There are different algorithms available when scheduling the processing of different tasks. Round robin scheduling originates from processing different tasks by a central processing unit (CPU) in your computer. The goal of round robin scheduling is to allocate a fixed amount of time to process a task before moving to the next one.

We’ve recently explored the Round Robin algorithm on a past Coding with Nylas livestream:

With the Nylas Scheduler, we implement a similar round robin algorithm which we will take a look at shortly.

Why use Python to create Round Robin Scheduling program

Python gives you the flexibility to programmatically create multiple calendars for your users (e.g. health care service providers) and source availability to their users. With the Smart Scheduler integration, you can use our API to create and manage calendars.

Using the Nylas Quickstart Guide

In this blog post, we will be modifying an existing Nylas Quickstart Guide to build round robin scheduling in python. First head over to Nylas.com and register to create an account or login.

We do not need to generate API keys to start using the quickstart guide! We can use the free tier that comes with 10 connected accounts. And each quickstart guide includes the Nylas API keys, so you can download and run the code locally.

Getting Started with Round Robin Scheduling in Python – Step by Step

Step 1. Start with the Scheduler Quickstart Guide

We are building off an existing Nylas Quickstart Guide for implementing the Nylas Scheduler:

The scheduler quickstart will provide us with a basic scheduling application from scratch that we can modify with the ability to apply round robin scheduling:

We start with an instant scheduler and will need to implement a smart scheduler integration. Check out our docs on the different scheduler implementations.

From the Nylas docs on scheduler implementations, we will need to pass an edit token to the user instead of the access token. An edit token will only allow the user to edit an existing calendar, whereas an access token can be used to both create and edit calendars. This is important from the perspective of who controls calendar creation. Using the smart scheduler integration, we can create calendars via the API and collect availability across all the different calendars using round robin scheduling.

Step 2. Create a Calendar for Each User

We will first modify the quickstart guide backend to create a new calendar for each user on authentication:

# file in /backend/server.py
# added to def exchange_code_for_token() method

  scheduler = nylas.scheduler.create()

  # Create an scheduler with a url slug for each user connected
  scheduler.name = "name"
  # Note that the slug must be unique for each user
  scheduler.slug = "scheduler-smart-1"
  scheduler.config = {
    "event": {
      "title": "Service",
    },
  }
  scheduler.access_tokens = [access_token]
  scheduler.save()

  return {
    'id': user['id'],
    'emailAddress': user['email_address'],
    'accessToken': user['access_token'],
    # We are passing back the editToken to the frontend
    'editToken': scheduler['edit_token']
  }

Now we’ve update the backend application to create a new scheduler for each user to modify.

Step 3: Allow user to modify a specific scheduler

Next we will modify the scheduler implementation on the frontend by modifying the authentication to only allow for editing a scheduler:

nylas.scheduler.show({
  auth: {
    //Instant Scheduler:access token to create/modify calendars
    //accessToken: sessionStorage.getItem('accessToken'),
     
    //Smart Scheduler: edit token to edit a single calendar
    pageEditToken: sessionStorage.getItem('editToken'),
  },
  // ...Rest of the scheduling configuration...

Now we’ve updated the frontend application to allow the user to modify a single calendar created for them.

Step 4. Request Availability using Round Robin Scheduling

Here we will further explore how to implement Round Robin scheduling. The idea behind creating a single scheduler for each user is that this allows us to collect availability across multiple users to share at once. So for example, if we are creating appointment availability for patients, we can collect the availability across all calendars using the calendar/availability endpoint:

  curl --location --request POST 'https://api.nylas.com/calendars/availability' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  # Add your access token for the calendar availability request
  --header 'Authorization: Bearer <ACCESS_TOKEN>' \
  --data-raw '{
    "duration_minutes": 15,
    "start_time": 1682678845,
    "end_time": 1682765245,
    "interval_minutes": 5,
    "emails": [
      "[email protected]",
      // ADD MORE EMAILS HERE
     ],
    "free_busy": [],
    "buffer": 30,
    "round_robin": "max-availability",
    "open_hours": [
      {
        "emails": [
          "[email protected]",
           // ADD MORE EMAILS HERE
        ],
        "days": [0, 1, 2, 3, 4],
        "timezone": "America/Los_Angeles",
        "start": "10:00",
        "end": "16:00",
        "object_type": "open_hours"
      }
  ]
}'

Here is an example response for calendar availability:

{
  "object": "availability",
  "order": [
    "[email protected]",
    "[email protected]"
  ],
  "time_slots": [
    {
      "emails": [
        "[email protected]"
      ],
      "end": 1682702100,
      "end_time": 1682702100,
      "object": "time_slot",
      "start": 1682701200,
      "start_time": 1682701200,
      "status": "free"
    },
    {
      "emails": [
        "[email protected]"
      ],
      "end": 1682702400,
      "end_time": 1682702400,
      "object": "time_slot",
      "start": 1682701500,
      "start_time": 1682701500,
      "status": "free"
    },
    {
      "emails": [
        "[email protected]"
      ],
      "end": 1682702700,
      "end_time": 1682702700,
      "object": "time_slot",
      "start": 1682701800,
      "start_time": 1682701800,
      "status": "free"
    },
    {
      "emails": [
        "[email protected]"
      ],
      "end": 1682703000,
      "end_time": 1682703000,
      "object": "time_slot",
      "start": 1682702100,
      "start_time": 1682702100,
      "status": "free"
    },
    {
      "emails": [
          "[email protected]"
      ],
      "end": 1682703300,
      "end_time": 1682703300,
      "object": "time_slot",
      "start": 1682702400,
      "start_time": 1682702400,
      "status": "free"
    },
    {
      "emails": [
        "[email protected]"
      ],
      "end": 1682703600,
      "end_time": 1682703600,
      "object": "time_slot",
      "start": 1682702700,
      "start_time": 1682702700,
      "status": "free"
    },
    {
      "emails": [
        "[email protected]",
        "[email protected]"
      ],
      "end": 1682722800,
      "end_time": 1682722800,
      "object": "time_slot",
      "start": 1682721900,
      "start_time": 1682721900,
      "status": "free"
    }
  ]
}

In the above we are using the round robin option of max-availability allows for showing the most available time slots. The alternative option is max-fairness which shares time slots by the least booked providers to spread the bookings across more providers.

In this section, we looked at the different steps to build round robin scheduler in python.

Build for Free!!

In this blog, we covered how to implement Round Robin Scheduling using Python and Nylas. You can sign up for Nylas for free and start building!

Continue building with Nylas by exploring different quickstart guides or visiting our developer documentation.

Related resources

How to build a CRM in 3 sprints with Nylas

What is a CRM? CRM stands for Customer Relationship Management, and it’s basically a way…

How to create an appointment scheduler in your React app

Learn how to create an appointment scheduler in your React app using Nylas Scheduler. Streamline bookings and UX with step-by-step guidance.

Beyond APIs: Designing elegant, smart Web Components

Dive into the world of smart Web Components for advanced API integration solutions. Design elegant, customizable elements to elevate user experiences.