<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Derrick Zziwa</title>
    <description>The latest articles on Forem by Derrick Zziwa (@derricktab).</description>
    <link>https://forem.com/derricktab</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F407564%2F3a53398f-f8f4-45e2-98e7-bd122ededc79.jpg</url>
      <title>Forem: Derrick Zziwa</title>
      <link>https://forem.com/derricktab</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/derricktab"/>
    <language>en</language>
    <item>
      <title>Safe Threading with SQLAlchemy in FastAPI</title>
      <dc:creator>Derrick Zziwa</dc:creator>
      <pubDate>Fri, 14 Feb 2025 19:07:32 +0000</pubDate>
      <link>https://forem.com/derricktab/safe-threading-with-sqlalchemy-in-fastapi-4e9e</link>
      <guid>https://forem.com/derricktab/safe-threading-with-sqlalchemy-in-fastapi-4e9e</guid>
      <description>&lt;p&gt;When building FastAPI applications that use SQLAlchemy for data persistence, you often need to perform background tasks—such as updating records or processing data—without blocking the main request. A common approach is to offload work to background threads. However, threading in Python comes with its own challenges, especially regarding SQLAlchemy sessions.&lt;/p&gt;

&lt;p&gt;In this article, we are going to understand the limitations of using threads with SQLAlchemy sessions, and then we’ll discuss how to safely address these challenges.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Are Threads?
&lt;/h2&gt;

&lt;p&gt;Threads are a way to perform multiple operations concurrently within a single process. They allow your application to handle tasks in parallel—such as processing I/O-bound operations—without waiting for one task to finish before starting another.&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage in FastAPI:
&lt;/h2&gt;

&lt;p&gt;In a FastAPI application, you might use threads to run background tasks so that the main API request isn’t delayed. For example, sending an email, updating a record, or doing non-critical processing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitations of Threads
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Not Truly Parallel (CPython):&lt;/strong&gt; &lt;br&gt;
Due to the Global Interpreter Lock (GIL), threads in CPython are most effective for I/O-bound tasks. CPU-bound tasks might not gain much speed-up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Shared State and Concurrency:&lt;/strong&gt;&lt;br&gt;
Threads share the same memory space, which can lead to complications when mutable shared state is not managed carefully.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thread Safety Issues:&lt;/strong&gt;&lt;br&gt;
Not every component is thread-safe. In particular, SQLAlchemy sessions are not thread-safe—using the same session in multiple threads can lead to unpredictable behavior and errors.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Solution: Isolate SQLAlchemy Sessions in Threads
&lt;/h2&gt;

&lt;p&gt;Instead of passing the existing session to a background thread, you should create a new session within that thread. This ensures that each thread has its own isolated session and avoids the discussed pitfalls. &lt;/p&gt;
&lt;h2&gt;
  
  
  Implementation Steps
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Define a Session Factory:&lt;/strong&gt;&lt;br&gt;
In your application, use SQLAlchemy’s sessionmaker to create a session factory. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# database.py
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

DATABASE_URL = "postgresql://user:password@localhost/dbname"
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Use the Factory in Background Tasks:
&lt;/h2&gt;

&lt;p&gt;When you need to run a background task (e.g., updating a record), create a new session inside that task:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# background_tasks.py

from database import SessionLocal
from config.logger_config import logger

def update_record_background(param):
    db = SessionLocal()  # Create a new session for this thread
    try:
        # Perform your database operations here
        # e.g., update a record or commit a transaction
        db.commit()
    except Exception as e:
        db.rollback()
        logger.error(f"Error in background update: {e}")
    finally:
        db.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Launch the Background Task:
&lt;/h2&gt;

&lt;p&gt;In your FastAPI endpoint or wherever the background task is needed, start a new thread without passing the main session:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import threading
from background_tasks import update_record_background

# When triggering the background task:
threading.Thread(target=update_record_background, args=(param_value,)).start()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Benefits of This Approach
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Thread Safety:&lt;/strong&gt;&lt;br&gt;
Each thread operates on its own session, eliminating race conditions and ensuring data consistency.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Isolation:&lt;/strong&gt;&lt;br&gt;
Background tasks do not interfere with the main request’s session, preventing unexpected state changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scalability:&lt;/strong&gt;&lt;br&gt;
This approach makes your application easier to scale, as each task uses an independent session.&lt;/p&gt;

&lt;p&gt;We’ve identified the core issues when sharing SQLAlchemy sessions between threads: thread safety concerns and unexpected state changes. The solution is to create a new session for each background task, ensuring isolation and stability in your FastAPI applications.&lt;/p&gt;

&lt;p&gt;Implementing this pattern not only resolves errors like IllegalStateChangeError but also leads to cleaner, more maintainable code. &lt;br&gt;
This strategy is applicable to any FastAPI application using SQLAlchemy and can be adapted for more complex asynchronous tasks using task queues if needed.&lt;/p&gt;

</description>
      <category>sqlalchemy</category>
      <category>fastapi</category>
      <category>python</category>
      <category>threading</category>
    </item>
    <item>
      <title>OpenSource Models Surpass GPT3.5 on Several Benchmarks</title>
      <dc:creator>Derrick Zziwa</dc:creator>
      <pubDate>Sun, 23 Jul 2023 16:34:05 +0000</pubDate>
      <link>https://forem.com/derricktab/opensource-models-surpass-gpt35-on-several-benchmarks-48c1</link>
      <guid>https://forem.com/derricktab/opensource-models-surpass-gpt35-on-several-benchmarks-48c1</guid>
      <description>&lt;p&gt;Unleashing the Power of Open Source AI to Redefine Possibilities&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flomht4490m198syjmje4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flomht4490m198syjmje4.jpg" alt="Stability AI Logo" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Stability.AI, in an audacious move to push the boundaries of AI capabilities, proudly presents FreeWilly - a groundbreaking open-source project aimed at challenging the mighty GPT-3.5. Developed in collaboration with CarperAI lab, FreeWilly emerges as a formidable Large Language Model (LLM) with exceptional reasoning abilities, poised to revolutionize the world of natural language processing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Empowering Open Source AI Research&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;FreeWilly's mission is to democratize AI research and foster open collaboration. Both FreeWilly1 and its successor FreeWilly2 are released under open-source licenses, encouraging the AI community to explore, innovate, and contribute to the future of language models. As staunch advocates of open access, Stability.AI is committed to making advancements in AI accessible to all.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Formidable Challenge to GPT-3.5&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Built on the strong foundation of the LLaMA 65B and LLaMA 2 70B models, FreeWilly1 and FreeWilly2 were carefully fine-tuned using a new synthetically-generated dataset. Stability.AI's team employed the Supervised Fine-Tune (SFT) approach in standard Alpaca format to refine these models to their peak performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Red-Teaming and Community Collaboration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Stability.AI leaves no stone unturned to ensure the safety and ethics of FreeWilly. Thorough internal red-teaming has been conducted to ensure the models remain polite and harmless. However, the community's participation in the red-teaming process is encouraged, as diverse perspectives can help strengthen the models and identify potential areas of improvement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Data-Driven Journey&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Drawing inspiration from Microsoft's trailblazing "Orca: Progressive Learning from Complex Explanation Traces of GPT-4" paper, Stability.AI took a novel approach to data generation. By creating a variant dataset with 600,000 meticulously curated high-quality instruction data points from Enrico Shippole's datasets, FreeWilly sets new standards in training efficiency. Rigorous filtering and careful curation ensured that evaluation benchmarks did not influence the models, allowing for a fair and robust evaluation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unleashing Unprecedented Performance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Internal evaluation using EleutherAI’s lm-eval-harness, along with AGIEval, reveals FreeWilly's exceptional performance in various domains. From intricate reasoning to linguistic subtleties, FreeWilly showcases unparalleled proficiency in tasks like Law and mathematical problem-solving.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Leading the Open LLM Leaderboard&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;FreeWilly's exceptional results were not only validated by Stability.AI researchers but also independently reproduced by Hugging Face on July 21st, 2023. These benchmarks catapult FreeWilly to the forefront of the open LLM leaderboard, making it a powerful contender in the AI landscape.&lt;/p&gt;

&lt;p&gt;_&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An Open Future of Possibilities&lt;br&gt;
_&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;FreeWilly1 and FreeWilly2 mark a transformative step forward in the world of open-source Large Language Models. By enhancing natural language understanding and enabling complex tasks, these models offer limitless possibilities and applications. Stability.AI extends its heartfelt gratitude to its team of passionate researchers, engineers, and collaborators whose unwavering dedication led to this remarkable milestone.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Source: https://lnkd.in/dNcY_9jW&lt;/code&gt;&lt;/p&gt;

</description>
      <category>chatgpt</category>
      <category>stability</category>
      <category>freewilly</category>
      <category>opensource</category>
    </item>
    <item>
      <title>The Dangers of Using the Same Database for Development and Production</title>
      <dc:creator>Derrick Zziwa</dc:creator>
      <pubDate>Sun, 23 Jul 2023 07:42:00 +0000</pubDate>
      <link>https://forem.com/derricktab/the-perils-of-using-the-same-database-for-development-and-production-216h</link>
      <guid>https://forem.com/derricktab/the-perils-of-using-the-same-database-for-development-and-production-216h</guid>
      <description>&lt;p&gt;In the world of software development, maintaining a clear distinction between the development and production environments is crucial for the success, stability, and security of an application. Unfortunately, some organizations overlook the importance of using separate databases for development and production, unwittingly exposing themselves to a myriad of potential dangers. In this article, we will delve into the various risks associated with using the same database for both development and production purposes, as well as explore the best practices for establishing a clear separation between these environments.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqx9n6nx0aj61pjzd1fof.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqx9n6nx0aj61pjzd1fof.png" alt="Development VS Production" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Data Integrity Risks&lt;/strong&gt;&lt;br&gt;
When development and production share the same database, the chances of data corruption and accidental data deletion increase significantly. During the development process, developers may run tests, experiments, or updates that can unintentionally affect critical data in the production environment. A bug or error during development could lead to the loss of valuable production data, leading to financial losses and customer dissatisfaction.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Security Vulnerabilities&lt;/strong&gt;&lt;br&gt;
Combining development and production databases can expose your application to security vulnerabilities. In the development phase, developers often have broader access to the database to facilitate testing and debugging. However, this level of access should not be granted in a production environment, where data privacy and security are of utmost importance. A compromised development database can provide a pathway for attackers to gain unauthorized access to sensitive production data, leading to potential data breaches and compliance violations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Performance Bottlenecks&lt;/strong&gt;&lt;br&gt;
Sharing a single database between development and production can lead to performance bottlenecks, affecting both environments. In the development phase, developers may conduct extensive testing, run heavy queries, and perform resource-intensive tasks that impact the performance of the production application. Additionally, large-scale testing in the development environment may lead to limited resources for the production environment, causing performance degradation and reduced responsiveness for end-users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Impact on Continuous Integration/Continuous Deployment (CI/CD)&lt;/strong&gt;&lt;br&gt;
For organizations practicing CI/CD, sharing a database between development and production can hinder the smooth integration and deployment process. Frequent changes in the development environment might not be compatible with the production database schema, leading to deployment failures and application downtime. A separate database for each environment allows developers to test new features and updates without affecting the stability of the production system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Difficulty in Debugging and Troubleshooting&lt;/strong&gt;&lt;br&gt;
When development and production data coexist in the same database, troubleshooting and debugging become complicated. Issues faced during development may not be reproducible in the production environment, or vice versa, due to data discrepancies. As a result, identifying the root cause of problems becomes more challenging and time-consuming, delaying the resolution process and increasing maintenance costs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices for Database Separation
&lt;/h2&gt;

&lt;p&gt;To mitigate the dangers of using the same database for development and production, organizations should adopt best practices for creating clear separation between these environments:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Employ a Staging Environment&lt;/em&gt;: Introduce a staging environment that closely resembles the production setup. This environment acts as an intermediary step between development and production, allowing comprehensive testing and validation before deploying changes to the production database.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Implement Access Control&lt;/em&gt;: Restrict access rights to the production database, granting only necessary permissions to specific individuals or roles. Developers should have access to the development database, but this should be limited in scope and different from the production environment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Regular Data Backups&lt;/em&gt;: Establish a robust data backup strategy for both development and production databases. Regularly backing up data ensures that in the event of data loss or corruption, recovery can be made without substantial damage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Automation and Version Control&lt;/em&gt;: Embrace automation and version control tools to manage database schema changes. Automated processes reduce the risk of human error and ensure consistency across environments.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Conclusion&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
The dangers of using the same database for development and production cannot be underestimated. From data integrity risks and security vulnerabilities to performance bottlenecks and deployment challenges, the potential consequences are severe. By implementing best practices for database separation, organizations can safeguard their data, maintain a secure application environment, and optimize the development and production workflows, ultimately leading to improved overall system reliability and performance.&lt;/p&gt;

</description>
      <category>database</category>
      <category>firebase</category>
      <category>production</category>
      <category>development</category>
    </item>
    <item>
      <title>Why You Should Learn Flutter, RIGHT NOW!!</title>
      <dc:creator>Derrick Zziwa</dc:creator>
      <pubDate>Thu, 27 Apr 2023 08:02:49 +0000</pubDate>
      <link>https://forem.com/derricktab/why-you-should-learn-flutter-right-now-1b6l</link>
      <guid>https://forem.com/derricktab/why-you-should-learn-flutter-right-now-1b6l</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7cxzh72fw46w39mbb78k.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7cxzh72fw46w39mbb78k.jpg" alt="Flutter App Development" width="780" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  WHAT IS FLUTTER?
&lt;/h2&gt;

&lt;p&gt;Simply put, Flutter is the future of Mobile App Development. And am not kidding. Flutter is a relatively new mobile application development framework that is quickly gaining popularity in the tech world. Created by Google, Flutter has been designed to help developers build high-performance, visually stunning mobile apps for multiple platforms including Android, iOS, Linux, Windows, and Web, all with a single codebase! Amazing isn't it?&lt;/p&gt;

&lt;h2&gt;
  
  
  WHY LEARN FLUTTER?
&lt;/h2&gt;

&lt;p&gt;Flutter offers a number of features that make it stand out from other mobile app development frameworks. Here are some of the key benefits of using Flutter for mobile app development:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Fast development:&lt;/strong&gt; Flutter is designed to make the mobile app development process as fast and easy as possible. The framework comes with a rich set of pre-built widgets that can be easily customized to fit the needs of any app. This allows developers to create high-quality mobile apps in significantly less time than it would take with other app development frameworks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Native Performance:&lt;/strong&gt; One of the key benefits of Flutter is that it delivers native performance on any platform. Flutter uses the Skia Graphics Engine to render UIs directly on the device without any bridges or web views. This means your app can run smoothly and access native features and services without any performance issues or compatibility problems. Flutter also supports hardware acceleration, 60fps animations, and platform-specific gestures, giving your app a natural and responsive feel.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Cross-platform development:&lt;/strong&gt; Because Flutter uses a single codebase for both Android and iOS platforms, developers can create mobile apps that work seamlessly on both platforms. This means that the app will have the same look and feel on both Android and iOS devices, reducing the need to create separate versions of the app for each platform.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Beautiful UI:&lt;/strong&gt; One of the biggest advantages of using Flutter is the ability to create visually stunning UI elements. Flutter comes with a wide range of pre-built widgets that can be easily customized to create beautiful, responsive UI designs that look great on any device.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Open-source:&lt;/strong&gt; Flutter is an open-source framework, which means that developers can easily access the source code and contribute to the development of the framework. This makes it a popular choice among developers who value community involvement and collaboration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Light Weight:&lt;/strong&gt; Flutter is built using the Dart programming language, which is optimized for high-performance. This means that Flutter apps run smoothly and quickly, even on older or less powerful devices. This makes it a popular choice for developers who want to create mobile apps that offer a top-quality user experience.&lt;/p&gt;

&lt;p&gt;Flutter is quickly becoming the future of mobile app development. Its combination of fast development, cross-platform compatibility, high-performance, beautiful UI, and open-source nature make it an attractive choice for developers looking to create top-quality mobile apps. If you're a developer looking to create mobile apps for Android, iOS, Windows, or Linux platforms, consider giving Flutter a try.&lt;/p&gt;

&lt;p&gt;You can learn more about Flutter at &lt;a href="https://flutter.dev/" rel="noopener noreferrer"&gt;https://flutter.dev/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>mobile</category>
      <category>google</category>
      <category>dart</category>
    </item>
  </channel>
</rss>
