1、什么是协程?--> yield 有什么作用?--> yield 实现协程底层是怎么实现的?--> yield from 又是什么?
协程,又称微线程,纤程。英文名Coroutine。
协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用。
子程序,或者称为函数,在所有语言中都是层级调用,比如A调用B,B在执行过程中又调用了C,C执行完毕返回,B执行完毕返回,最后是A执行完毕。
所以子程序调用是通过栈实现的,一个线程就是执行一个子程序。
import concurrent.futures | |
import urllib.request | |
import requests | |
import os | |
headers = { | |
'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"} | |
URLS = ['http://www.jianshu.com/', |
import unittest | |
def sift_down(lst, top, end): | |
pos = end | |
newitem = lst[pos] | |
while pos > top: | |
parentpos = (pos - 1) >> 1 | |
parent = lst[parentpos] | |
if newitem < parent: |
from collections import namedtuple | |
Result = namedtuple('Result', 'Count Averge') | |
def averger(): | |
count = 0 | |
total = 0 | |
avg = None | |
while True: | |
term = yield avg |
import hashlib | |
from concurrent import futures | |
import os | |
import time | |
from itertools import chain | |
from functools import wraps | |
def timeit(func): | |
@wraps(func) |
import hashlib | |
def md5(fname): | |
hash_md5 = hashlib.md5() | |
with open(fname, "rb") as f: | |
for chunk in iter(lambda: f.read(4096), b""): | |
hash_md5.update(chunk) | |
return hash_md5.hexdigest() | |
import concurrent.futures | |
import urllib.request | |
import requests | |
import os | |
headers = { | |
'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"} | |
URLS = ['http://www.jianshu.com/', |
1、什么是协程?--> yield 有什么作用?--> yield 实现协程底层是怎么实现的?--> yield from 又是什么?
协程,又称微线程,纤程。英文名Coroutine。
协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用。
子程序,或者称为函数,在所有语言中都是层级调用,比如A调用B,B在执行过程中又调用了C,C执行完毕返回,B执行完毕返回,最后是A执行完毕。
所以子程序调用是通过栈实现的,一个线程就是执行一个子程序。
import subprocess | |
import asyncio | |
from itertools import chain | |
from functools import wraps | |
import concurrent | |
import time | |
import readxml | |
EXE = r"\\ccpsofsep\am_s1\users\v-taotli\code\CollectData\CollectData\CollectData\bin\Debug\CollectData.exe" |
import random | |
def heap_sort(ints): | |
heapify(ints) | |
res = [] | |
while ints: | |
res.append(heappop(ints)) | |
return res | |
import random | |
def merge_sort(ints): | |
length = len(ints) | |
if length < 2: | |
return ints | |
mid = length >> 1 | |
left, right = merge_sort(ints[:mid]), merge_sort(ints[mid:]) | |
left_count, right_count = len(left), len(right) |