User Tools

Site Tools


python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

python [2022/01/25 02:49] – created - external edit 127.0.0.1python [2024/05/23 11:04] (current) jhagstrand
Line 633: Line 633:
   $ import uuid   $ import uuid
   $ uuid.uuid4() # generate a unique id   $ uuid.uuid4() # generate a unique id
 +
 ===== Concurrency ===== ===== Concurrency =====
  
Line 700: Line 701:
  
 NOTE:  Websockets are built on asyncio. NOTE:  Websockets are built on asyncio.
- 
-===== matplotlib ===== 
- 
-import matplotlib.pyplot as plt 
- 
- 
- 
- frequency distribution is a table, 2 columns: value, frequency 
- histogram is a bar graph 
- 
-1. an array of 1000 persons, one value per person 
-2. frequency distribution or values and frequency of each value 
-3. bar graph 
- 
-numpy.histogram() 
-matplotlib.pyplot.hist() 
- 
-matplotlib.p)yplot.axes.Axes.bar() 
- 
-evidently, in an animation, we do not re-plot 
-instead we change the data, and the plot is changed thereby 
  
  
-three examples of animations +===== software architecture options =====
-1. numpy.histogram, preparing and returning a bart_container.patches, blit=True +
-2. ln plt.plot(), in loop ln.set_data(x,y), return ln, blit=True +
-3. anim.py oscillation decay, blit=False+
  
-pylab is deprecated +synchronous 
-pyplot is collection of functions that make matplotlib work like MATLAB +  set of tasks must be executed one-at-a-time in a specific order  * 
-fig, ax = plt.subplots()+
  
-plt.plot() # possible, plots within the current ax +async  
-ax.plot()  # betterplots within the specified ax+  * a set of tasks can be run in any orderand maybe simultaneously  
 +  * each task may be interruptible
  
 +blocking
 +  * with a synchronous design, long-running tasks run until completion, blocking any other tasks 
  
 +  * python's native http libraries all block between request and response
 +  *   * over 1700 python libraries for http, and every one of them blocks:
 +  *     * requests.get( url) blocks
 +  *     * http.client.getResponse() blocks
 +  *     * urllib3.PoolManager().request() blocks
 +  *     * etc
 +  * this is the opposite of http's design intention: an unthinkable  * implementation
  
 +  * the requests library has the tagline: "http for humans"
 +  *   * this implies:
 +  *     * the library authors find http progamming difficult
 +  *     * so they are here to save the day by simplifying it
 +  *   * in fact:
 +  *     * a better tagline would be: "http for amateurs"
  
-# using pyplot +event-driven 
-fig = plt.gcf()  # get (or create) current figure +  * event-driven design is one example of async design 
-ax  = plt.gca()  # get current axes+  * using callbacks or signals 
 +  * make an http request and return immediately 
 +  * on receipt of the response, set a signal or call a callback
  
-fig,ax = plt.subplots()+cooperative multitasking 
 +  * asyncio is an example of cooperative multitasking 
 +  * the programmer inserts an await directive into a function 
 +  *   * where it is appropriate to interrupt the function and allow other tasks to run
  
-ax.plot() +preemptive multitasking 
-ax.bar() +  * the os decides when to interrupt a task
-ax.scatter() +
-ax.hist()+
  
 +asyncio
 +  * a python library
 +  * implements cooperative multitasking
 +  * def async functionname(): - defines an interruptible function
 +  * await - a directive placed within an async function to yield cpu to other async functions
 +  * all threads run in one process, which means 
 +  *   * they can theoretically run async (non-blocking), but not simultaneously
 +  * the python http libraries do not allow a place to put the await directive
 +  *   * between the request and the response, 
 +  *   * so the asyncio library does not solve the http blocking problem
 +  * because we call requests.get() within a subprocess
 +  *   * the combinatino of asyncio and subprocess might work
  
-ax.hist calls np.histogram to calculate the frequency distribution, a 2D array,  +threading 
-builds a BarContainer object and plots the bars onto the ax+  * a python library 
 +  * programmer can start multiple threads and assign functions to each thread 
 +  * all threads run in one process 
 +  * the global interpreter lock (GIL) - in CPython, a predominate python interpreter -  
 +  *   * prevents two threads from executing python code simultaneously
 +  * therefore, threads can execute async (non-blocking), but not simultaneously
  
-# to print table, use zip to transpose rows and columns +multiprocessing 
-table = tuple(zip(fd[1],fd[0])) +  * python library 
-for row in table: +  * similar to threading, but using "process" instead of "thread" 
-        print(*row)+  * each process can be assigned to a different cpu or core,  
 +    * allowing multiple tasks to run simultaneously
  
  
python.1643096959.txt.gz · Last modified: 2022/01/25 02:49 by 127.0.0.1

Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Public Domain Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki