Sharing my learnings....

Month: December 2023

PYTHONPATH in a nutshell

I was running a Django project, and I had to use a GitHub repo as a third-party dependent in the project by cloning it and incorporating it inside the project. But, for some reason, I couldn’t run the project because of the Module Not Found error which caused by the usage inside third-party folder. So, I had to add that third-party folder to PYTHONPATH variable to run the project.

This is a brief article about what is PYTHONPATH and its intro:

Imagine your computer is like a big library with lots of books, which are like the programs and files on your computer. Now, when you want to run a Python program, it needs to use some special books (libraries or modules) to work correctly.

PYTHONPATH is like a list of favorite aisles in the library where these special books are. When you tell Python, “Hey, I want to run this program,” Python looks at the PYTHONPATH to know where to find the special books (modules) it needs to run your program.

Why to Set PYTHONPATH:

  1. To Find Special Books Easily: Sometimes, you have your own special books (modules) not in the usual aisles. By adding their locations to PYTHONPATH, you tell Python exactly where to find them.
  2. To Share and Use Different Books: If you have many different projects or programs, they might need different special books. You can change PYTHONPATH for each one, so Python knows where to look for the right books for each project.

Usage of PYTHONPATH:

When you run a Python program, and it needs a special book (module or library), it will start looking in the aisles listed in PYTHONPATH. If it finds the right book there, it will use it! If not, it might tell you it can’t find it or look in the default places it knows.

In summary, PYTHONPATH is used to guide Python on where to find the extra special books (modules and libraries) it needs to run your programs, especially when those books aren’t in the usual places!

Django Views are Synchronous, You can’t have Asynchronous things inside views

python=django-learning

The core issue I was facing was related to the inherent nature of how web servers and Django handle requests and responses, especially in relation to performing time-consuming tasks like sending emails and Slack messages within a Django view.

Understanding the Problem:

  1. Synchronous Nature of Django Views:
    • Django views, by default, operate synchronously. This means that when a request hits a Django view, the server processes the request in a linear, blocking fashion. It executes each line of code in the view one by one, and the response to the client (frontend) is not sent back until the entire view function completes its execution.
  2. Frontend Loading Time:
    • When you include operations like sending emails or Slack messages directly in your Django view, these operations are executed as part of the request-processing pipeline. Since these tasks can be time-consuming (network I/O, waiting for external API responses), they block the completion of the view function. As a result, the response is delayed until these tasks are finished, leading to increased loading times on the frontend.
  3. Asynchronous Functions in Django:
    • Even if I make certain functions asynchronous within the Django view (using async def and await), it doesn’t change the fundamental synchronous nature of the view’s response cycle. The view still waits for all operations, including the asynchronous ones, to complete before sending back a response. This means that making functions asynchronous inside a view won’t reduce the frontend loading time if these tasks are part of the request-response cycle.
  4. Event-Driven Architecture Approach:
    • You mentioned an alternative approach using an event-driven architecture, where an event is published to a queue, and a separate consumer service handles the notifications. This method is indeed a way to offload time-consuming tasks from the request-response cycle. The view would quickly publish an event to the queue and then immediately respond to the frontend, significantly reducing loading times. However, this approach introduces complexity, such as setting up and managing a message queue and a consumer service, and it might incur additional costs.
  5. Other Solutions – Background Task Processing:
    • Another common solution in Django is to use a background task processing system like Celery. With Celery, you can quickly dispatch time-consuming tasks to be handled asynchronously outside of the request-response cycle. This allows your view to respond immediately, while the tasks like sending emails or Slack messages are processed in the background.

Conclusion:

  • In summary, simply converting functions within a Django view to asynchronous won’t solve the issue of frontend loading times when performing time-consuming tasks within the view. The response to the client is still delayed until these tasks complete.
  • To effectively reduce frontend loading times, you need to offload these time-consuming tasks from the request-response cycle. This can be achieved using an event-driven architecture with a message queue and a consumer service or by implementing a background task processing system like Celery.

My 2023 and 2024 Planning

hmm, 2023 overall was a good year for me. There were a lot of days I missed my diet food and to do exercises frequently. To come against this barrier, I enrolled to a Gym in Wellawatta, Colombo near my boarding place. I feel a block in my active lifestyle during my days in Colombo for the office days. Whenever I am back in Jaffna, I loose hopes in doing exercises and maintain healthy and active lifestyle. What I am planning in 2024 is to go for gym during the days in Colombo and do home workouts when I am in Jaffna (As Simple as that).

But still, there is a question on how I am going to be consistent on this? Will have a hashtag something like #MyHealth2024 & #MyGoal2024 and post in Twitter (X).

There are lot of pending things which I was planning for a long day and expecting it to make it as a Goal to be finished in 2024.

  1. This year, I purchased Namaste React course from Akshay Shainy. I took it a serious course and finished till testing the application. But later, due to lack of motivation I left it from there. Will have to focus on building projects on top of it and complete the course fully. – The Goal for #MyGoal2024
  2. Purchased a life-time course access to codedamn, a good looking and decent course setup from the people who created it. May be will have a frequent look from it.
  3. Planning to do some Python + Django project based learning for next year. The Goal for #MyGoal2024
  4. AWS Certified Developer Associate Exam. Already this year, took the exam but got 629 which is near to get passed (720). Will try a new shot and get the certification #MyGoal2024
  5. Need to learn more deeper on Golang. Finishing up the Usemy Golang course #MyGoal2024
  6. Get a good understanding about React Native and get release an app #MyGoal2024

How was 2023?

  • Learnt a lot of work related dos and don’ts
  • Got iPhone 15 Pro and Ai pods Pro 2 this year as an installment from the monthly salary. Thanks to JavaScript and React eco-system. It’s not possible without you guys…
  • I think I have a lot of procrastination things on my mind. Working on lot of things paralelly and stressing out myself. I should think which one should I prioritize and give more importance to my loved one(s). I will be more strict on this from next year onwards.
  • Have to plan up my work annual leaves and be organized from next year onwards. Take care of health alot..

Let’s see how it goes…