Git Product home page Git Product logo

advanced-python-topics's Introduction

Lists:


Multi threading vs Multi Processing

Process

  • separate memory space -> memory not shared between processes
  • Heavyweight
  • One GIL for each process
  • IPC is more complicated

Thread

  • leightweight
  • great for I/O bound tasks
  • one thread at a time
  • threads share memory
  • limited by GIL: one thread at a time
  • not interruptable/killable
  • careful with race conditions -> change one memory space at the same time

GIL:

  • Global Interpreter Lock
  • a lock that allows only on thread at a time to execute in py
  • avoid multiprocessing
  • reference counting

Threading

  • Daemon thread: a thread that dies after the main thread dies
  • By default, threads are not daemon threads
thread.daemon = True  # demon thread -> background thread that will die when the main thread diethread.daemon = True  # demon thread -> background thread that will die when the main thread die
  • Race condition: two threads accessing the same resource at the same time
  • To deal with race conditions: lock and release resources or using context managers (with)
def increase(lock: Lock):
    global database_value

    lock.acquire()
    local_cp = database_value

    # processing
    local_cp += 1
    
    time.sleep(0.1)
    database_value = local_cp
    lock.release()
# Or with
def increase(lock: Lock):
    global database_value
    
    with lock:
        local_cp = database_value

        # processing
        local_cp += 1
        
        time.sleep(0.1)
        database_value = local_cp

Queues:

  • thread-safe env
  • q.join(): block main thread until all queue items are done and processed
  • q.task_done(): marks a thread as done

Multiprocessing:

Share data between processes:

  • Processes do not share same memory

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.