python
Differences
This shows you the differences between two versions of the page.
| python [2022/01/25 02:49] – created - external edit 127.0.0.1 | python [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 | ||
| - | |||
| - | |||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | 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, | + | |
| - | 2. ln = plt.plot(), in loop ln.set_data(x, | + | |
| - | 3. anim.py oscillation decay, blit=False | + | |
| - | pylab is deprecated | + | synchronous |
| - | pyplot is a collection | + | |
| - | fig, ax = plt.subplots() | + | |
| - | plt.plot() # possible, plots within the current ax | + | async |
| - | ax.plot() | + | |
| + | * each task may be interruptible | ||
| + | blocking | ||
| + | * with a synchronous design, long-running tasks run until completion, blocking any other tasks | ||
| + | * python' | ||
| + | * * over 1700 python libraries for http, and every one of them blocks: | ||
| + | * | ||
| + | * | ||
| + | * | ||
| + | * | ||
| + | * this is the opposite of http's design intention: an unthinkable | ||
| + | * the requests library has the tagline: "http for humans" | ||
| + | * * this implies: | ||
| + | * | ||
| + | * | ||
| + | * * in fact: | ||
| + | * | ||
| - | # using pyplot | + | event-driven |
| - | fig = plt.gcf() | + | |
| - | ax = plt.gca() | + | * using callbacks |
| + | | ||
| + | * 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(): | ||
| + | * 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), | ||
| + | * 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 | + | threading |
| - | builds a BarContainer object and plots the bars onto the ax | + | * a python library |
| + | * programmer can start multiple threads and assign functions | ||
| + | * 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), | ||
| - | # to print a table, use zip to transpose rows and columns | + | multiprocessing |
| - | table = tuple(zip(fd[1],fd[0])) | + | * a python library |
| - | for row in table: | + | * similar |
| - | 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