Choice between multithreading and multi-processing: When to use what

Choice between multithreading and multi-processing: When to use what

Introduction

Single threaded and single process solution is normal practice. For example, if you open the text editor, it will start a single threaded process. For each file, it will start a new text editor instance. This works well.


In a typical scenario, for high performance and scalable solution, designer needs to use multiple threads which runs on multi-core cpu in parallel. For example, consider online shopping website. The load on http web process depends on number of people accessing the site. To provide better throughput and response time, designer of such site must consider parallelism of operation.

Comparison between Thread and Process


?When to select multithreading or multi process

Choice between multithreading and multi-process depends on the problem at hand. In case parallelism is needed and the parallel running entity are related in terms of data, then multithreading can be the first choice of evaluation. We can even consider hybrid model. Below there are few interesting use-cases.


Use-case1

If we design a http web process, a designated master thread can handle http connection. Multiple worker thread can serve the http request. Note that in this case,?

  • http request data is shared between master thread and worker thread
  • Worker thread is not memory hungry.

Use-case2

In this use-case, programmer needs to collect info from various sources and then store these info to database.?

Here there are two components:?

  • Collectors which fetches info from various sources
  • Database manager which stores data?


For collector, multiple processes makes more sense. I am telling this since?

  • It allows horizontal scalability. Note that multiple collectors can separately run in different machines and communicate with the database to write the data.
  • Collectors runs periodically and so, there can be async single threaded implementation which makes high performance code which is simple to maintain


For database management, single process with multiple threads makes more sense since

  • It avoid inter process lock to access database.
  • database manager needs to provide better response time and so, it needs to use multiple threads.


Reference

https://stackoverflow.com/questions/5440128/thread-context-switch-vs-process-context-switch

https://stackoverflow.com/questions/3044580/multiprocessing-vs-threading-python

https://blogs.datalogics.com/2013/09/25/threads-vs-processes-for-program-parallelization/

要查看或添加评论,请登录

Deepak Kumar的更多文章

  • Role of DBSCAN in machine learning

    Role of DBSCAN in machine learning

    Why to read this? Density-based spatial clustering of applications with noise (DBSCAN)is a well-known data clustering…

  • Artificial Narrow Intelligence

    Artificial Narrow Intelligence

    About ANI ANI stands for "Artificial Narrow Intelligence." ANI refers to artificial intelligence systems that are…

  • Federated learning and Vehicular IoT

    Federated learning and Vehicular IoT

    Definition Federated Learning is a machine learning paradigm that trains an algorithm across multiple decentralised…

  • An age old proven technique for image resizing

    An age old proven technique for image resizing

    Why to read? Anytime, was you curious to know how you are able to zoom small resolution picture to bigger size?…

    1 条评论
  • Stock Market Volatility Index

    Stock Market Volatility Index

    Why? Traders and investors use the VIX index as a tool to gauge market sentiment and assess risk levels. It can help…

  • The case for De-normalisation in Machine learning

    The case for De-normalisation in Machine learning

    Why? The need for inverse normalization arises when you want to interpret or use the normalized data in its original…

    1 条评论
  • Kubernetes complements Meta-verse

    Kubernetes complements Meta-verse

    Motivation The #metaverse is a virtual world or space that exists on the #internet . It's like a big interconnected…

    1 条评论
  • Which one offers better Security- OSS or Proprietary software

    Which one offers better Security- OSS or Proprietary software

    Motivation World is using so many OSS. Apache Kafka is a core part of our infrastructure at LinkedIn Redis is core part…

  • Why chatGPT/LLM should have unlearning capability like human has..

    Why chatGPT/LLM should have unlearning capability like human has..

    Executive Summary Do you know, chatGPT/LLM has this open problem to solve. This problem(unlearn) has potential to…

    1 条评论
  • Gini index for ML (Performance measurement and many more..)

    Gini index for ML (Performance measurement and many more..)

    Motivation You have developed machine learning model. What is next? You definitely want to check its performance.

    1 条评论

社区洞察