Mysql metadata lock(MDL)

metadata lock5.5.3 引入

当事务访问处理过程中执行DDL操作将会等到metadata lock执行完毕才能操作

如果没有访问这个表 是可以被ddl操作的

没数据的情况下可以ddl操作

在事务里不要用DDL 有隐式提交 

:会话2执行drop 操作 wait 再开会话3执行select也是操作不了的 要等会话2的drop执行完了才能查询

所以很容易把线上库卡住

解决办法:kill掉 drop

线上DB不要轻易做alter table 

在生产中做DDL操作时请冷静思考一下


演示:

begin这里只是为了模拟事务

如果事务中一个sql跑的很慢也会出现这种情况

我开启了2个会话

image.png

在会话1里申明一个显示的事务

在会话2里drop leo_copy表

image.png

就会看到这个时候drop不掉le_copy表

state是“Waiting for table metadata lock”

FLUSH TABLES WITH READ LOCK 和 LOCK TABLES比较

1、FLUSH TABLES WITH READ LOCK

这个命令是全局读锁定,执行了命令之后所有库所有表都被锁定只读。一般都是用在数据库联机备份,这个时候数据库的写操作将被阻塞,读操作顺利进行。解锁的语句也是unlock tables。

2、LOCK TABLES tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}

这个命令是表级别的锁定,可以定制锁定某一个表。例如: lock  tables test read; 不影响其他表的写操作。解锁语句也是unlock tables。

这两个语句在执行的时候都需要注意个特点,就是 隐式提交的语句。在退出mysql终端的时候都会隐式的执行unlock tables。也就是如果要让表锁定生效就必须一直保持对话。

3、MYSQL的read lock和wirte lock

read-lock:  允许其他并发的读请求,但阻塞写请求,即可以同时读,但不允许任何写。也叫共享锁

write-lock: 不允许其他并发的读和写请求,是排他的(exclusive)。也叫独占锁

​python3 pandas基本操作

#encodeing=utf-8
import numpy as np
import pandas as pd

s = pd.Series([i*2 for i in range(1,11)])
dates=pd.date_range("20170301", periods=8)
df=pd.DataFrame(np.random.randn(8,5),index=dates,columns=list("ABCDE"))
print(df)

随机生成8行5列 以dates作为index,ABCDE作为列

image.png

#打印前3行
print(df.head(3))

#打印后三行
print(df.tail(3))

image.png

#打印索引值
print(df.index)

#打印value
print(df.values)

image.png

#转制 索引和列转换
print(df.T)

image.png

print(df.sort(columns="C"))

我测试sort的时候尽然报错 可能是python3对排序重新定义了函数名

image.png

print(df.sort_index(axis=1,ascending=False))

image.png

print(df.describe())

image.png

#切片
print(df["A"])
print(type(df["A"]))

image.png

print(df[:3])
print("=============================================================")
print(df["20170301":"20170304"])
print("=============================================================")
print(df.loc[dates[0]])

image.png

print(df.loc["20170301":"20170304",["B","D"]])
print("=============================================================")
print(df.at[dates[0],"C"])

image.png

#通过下标进行选择
print(df.iloc[1:3,2:4])

image.png

#填条件进行筛选
print(df[df.B>0][df.A<0])
print("=============================================================")
print(df[df>0])
print("=============================================================")
print(df[df["E"].isin([1,2])])

image.png

df.iat[1,1]=1
print(df)

df.loc[:,"D"]=np.array([4]*len(df))
print(df)

df2=df.copy()
df2[df2>0]=-df2
print(df2)
df1=df.reindex(index=dates[:4],columns=list("ABCD")+["G"])
df1.loc[dates[0]:dates[1],"G"]=1
# print(df1)

#丢弃NaN的行
print(df1.dropna())

#填充NaN
print(df1.fillna(value=2))

image.png

image.png

image.png

image.png

image.png

http://www.imooc.com/video/14994

#encodeing=utf-8
import sys
import os
import re
import numpy as np
import pandas as pd
from pandas import Series, DataFrame, Panel

logfile = 'www.xxxxxx.com-access_log-20170521'

with open(logfile, 'r') as fo:
	log_list = []
	for line in fo:
		regex = '([(\d\.)]+) - - \[(.*?)\] "(.*?)" (\d+) (\d+|-)'
		rline = re.match(regex, line).groups()
		log_list.append(rline)

indexs=['IP','Time','Result','Status','No.']

df = DataFrame(log_list,columns=indexs)
print(df)
# print(df[df['IP'] == '42.120.160.97'])

image.png

像sql一样使用pandas 可以参考下面这篇文章

http://www.cnblogs.com/en-heng/p/5630849.html

python3 模拟登陆

import urllib.request, urllib.parse, urllib.error
import http.cookiejar

login_url = 'http://blog.leokim.cn/wp-login.php'
values = {'log':'xxxxxxx','pwd':'xxxxxxxxxxxxx'}
postdata = urllib.parse.urlencode(values).encode()
user_agent = r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
headers = {'User-Agent': user_agent, 'Connection': 'keep-alive'}

cookie_filename = 'cookie.txt'
cookie = http.cookiejar.MozillaCookieJar(cookie_filename)
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)

request = urllib.request.Request(login_url, postdata, headers)
try:
    response = opener.open(request)
    page = response.read().decode()
    # print(page)
except urllib.error.URLError as e:
    print(e.code, ':', e.reason)

cookie.save(ignore_discard=True, ignore_expires=True)  # 保存cookie到cookie.txt中
print(cookie)

for item in cookie:
    print('Name = ' + item.name)
    print('Value = ' + item.value)


get_url = 'http://blog.leokim.cn/wp-admin/edit.php'  # 利用cookie请求访问另一个网址
get_request = urllib.request.Request(get_url, headers=headers)
get_response = opener.open(get_request)
print(get_response.read().decode())

用cookie直接访问

import urllib.request, urllib.parse, urllib.error
import http.cookiejar

cookie_filename = 'cookie.txt'
cookie = http.cookiejar.MozillaCookieJar(cookie_filename)
cookie.load(cookie_filename, ignore_discard=True, ignore_expires=True)

handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)

get_url = 'http://blog.leokim.cn/wp-admin/tools.php'
get_request = urllib.request.Request(get_url)
get_response = opener.open(get_request)
print(get_response.read().decode())

Python3 urllib使用方法

我自己码了一遍结果没注意浏览器返回上一页了 艹

直接复制粘贴了吧 再搞一遍太麻烦了

1、最简单

import urllib.request
response = urllib.request.urlopen('http://python.org/')
html = response.read()

2、使用 Request

import urllib.request
req = urllib.request.Request('http://python.org/')
response = urllib.request.urlopen(req)
the_page = response.read()

3、发送数据

#! /usr/bin/env python3
import urllib.parse
import urllib.request
url = 'http://localhost/login.php'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {
'act' : 'login',
'login[email]' : 'yzhang@i9i8.com',
'login[password]' : '123456'
}
data = urllib.parse.urlencode(values)
req = urllib.request.Request(url, data)
req.add_header('Referer', 'http://www.python.org/')
response = urllib.request.urlopen(req)
the_page = response.read()
print(the_page.decode("utf8"))

4、发送数据和header

#! /usr/bin/env python3
import urllib.parse
import urllib.request
url = 'http://localhost/login.php'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {
'act' : 'login',
'login[email]' : 'yzhang@i9i8.com',
'login[password]' : '123456'
}
headers = { 'User-Agent' : user_agent }
data = urllib.parse.urlencode(values)
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req)
the_page = response.read()
print(the_page.decode("utf8"))

5、http 错误

#! /usr/bin/env python3
import urllib.request
req = urllib.request.Request('http://www.111cn.net ')
try:
urllib.request.urlopen(req)
except urllib.error.HTTPError as e:
print(e.code)
print(e.read().decode("utf8"))

6、异常处理1

#! /usr/bin/env python3
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
req = Request("http://www.111cn.net /")
try:
response = urlopen(req)
except HTTPError as e:
print('The server couldn't fulfill the request.')
print('Error code: ', e.code)
except URLError as e:
print('We failed to reach a server.')
print('Reason: ', e.reason)
else:
print("good!")
print(response.read().decode("utf8"))

7、异常处理2

#! /usr/bin/env python3
from urllib.request import Request, urlopen
from urllib.error import  URLError
req = Request("http://www.111cn.net /")
try:
response = urlopen(req)
except URLError as e:
if hasattr(e, 'reason'):
print('We failed to reach a server.')
print('Reason: ', e.reason)
elif hasattr(e, 'code'):
print('The server couldn't fulfill the request.')
print('Error code: ', e.code)
else:
print("good!")
print(response.read().decode("utf8"))

8、HTTP 认证

#! /usr/bin/env python3
import urllib.request
# create a password manager
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
# Add the username and password.
# If we knew the realm, we could use it instead of None.
top_level_url = "https://www.111cn.net /"
password_mgr.add_password(None, top_level_url, 'rekfan', 'xxxxxx')
handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
# create "opener" (OpenerDirector instance)
opener = urllib.request.build_opener(handler)
# use the opener to fetch a URL
a_url = "https://www.111cn.net /"
x = opener.open(a_url)
print(x.read())
# Install the opener.
# Now all calls to urllib.request.urlopen use our opener.
urllib.request.install_opener(opener)
a = urllib.request.urlopen(a_url).read().decode('utf8')
print(a)

9、使用代理

#! /usr/bin/env python3
import urllib.request
proxy_support = urllib.request.ProxyHandler({'sock5': 'localhost:1080'})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
a = urllib.request.urlopen("http://www.111cn.net ").read().decode("utf8")
print(a)

10、超时

#! /usr/bin/env python3
import socket
import urllib.request
# timeout in seconds
timeout = 2
socket.setdefaulttimeout(timeout)
# this call to urllib.request.urlopen now uses the default timeout
# we have set in the socket module
req = urllib.request.Request('http://www.111cn.net /')
a = urllib.request.urlopen(req).read()
print(a)

python 下载远程视频到本地

#!/usr/bin/env python
#-*-coding:utf-8-*-'
#Filename:download_file.py
 
import sys,os
import urllib.request
 
def urlcallback(a,b,c):
    """
        call back function
        a,已下载的数据块
        b,数据块的大小
        c,远程文件的大小
    """
    prec=100.0*a*b/c
    if 100 < prec:
        prec=100
    print("%.2f%%"%(prec,))
     
def main(argv):
    """
        main
    """
    print("start...")
    urllib.request.urlretrieve("https://bd.phncdn.com/videos/201210/15/6345721/vl_720_831k_6345721.mp4?ipa=47.52.4.119&rs=146&ri=1200&s=1495199710&e=1495206910&h=6a18d80e510cc3deb9f455d39c82931c"\
                      ,"1.mp4"\
                      ,urlcallback)
    print("end...")
     
if __name__=="__main__":
    main(sys.argv[1:])

猴子补丁(monkey patch)

monkey patch指的是在运行时动态替换,一般是在startup的时候.

用过gevent就会知道,会在最开头的地方gevent.monkey.patch_all();把标准库中的thread/socket等给替换掉.这样我们在后面使用socket的时候可以跟平常一样使用,无需修改任何代码,但是它变成非阻塞的了.

之前做的一个游戏服务器,很多地方用的import json,后来发现ujson比自带json快了N倍,于是问题来了,难道几十个文件要一个个把import json改成import ujson as json吗?

其实只需要在进程startup的地方monkey patch就行了.是影响整个进程空间的.

同一进程空间中一个module只会被运行一次.

下面是代码.


main.py

import jsonimport ujsondef monkey_patch_json():
	json.__name__ = 'ujson'
	json.dumps = ujson.dumps
	json.loads = ujson.loads

monkey_patch_json()
print 'main.py',json.__name__
import sub

sub.py

import jsonprint 'sub.py',json.__name__

运行main.py,可以看到都是输出'ujson',说明后面import的json是被patch了的.

最后,注意不能单纯的json = ujson来替换.

python 协程

#生产者
def consumer():
    r = ''
    while True:
        n = yield r
        if not n:
            return
        print('[CONSUMER] Consuming %s...' % n)
        r = '200 OK'


def produce(c):
	#启动生成器
    c.send(None)
    n = 0
    while n < 5:
        n = n + 1
        print('[PRODUCER] Producing %s...' % n)

        #切换到consumer执行
        r = c.send(n)

        print('[PRODUCER] Consumer return: %s' % r)
    c.close()

c = consumer()
produce(c)

注意到consumer函数是一个generator,把一个consumer传入produce后:

1. 首先调用c.send(None)启动生成器;

2. 然后,一旦生产了东西,通过c.send(n)切换到consumer执行;

3. consumer通过yield拿到消息,处理,又通过yield把结果传回;

4. produce拿到consumer处理的结果,继续生产下一条消息;

5. produce决定不生产了,通过c.close()关闭consumer,整个过程结束。

整个流程无锁,由一个线程执行,produce和consumer协作完成任务,所以称为“协程”,而非线程的抢占式多任务。

python 多线程

import time, threading

#新线程执行的代码:
def loop():
	print('thread %s is running...' % threading.current_thread().name)
	n = 0
	while n < 5:
		n = n + 1
		print('thread %s >>> %s' % (threading.current_thread().name, n))
		time.sleep(1)
	print('thread %s ended.' % threading.current_thread().name)

print('thread %s is running...' % threading.current_thread().name)
t1 = threading.Thread(target=loop, name='LoopThread1')
t1.start()
t2 = threading.Thread(target=loop, name='LoopThread2')
t2.start()
t3 = threading.Thread(target=loop, name='LoopThread3')
t3.start()
t4 = threading.Thread(target=loop, name='LoopThread4')
t4.start()

t1.join()
t2.join()
t3.join()
t4.join()
print('thread %s ended.' % threading.current_thread().name)

image.png

由于任何进程默认就会启动一个线程,我们把该线程称为主线程,主线程又可以启动新的线程,Python的threading模块有个current_thread()函数,它永远返回当前线程的实例。主线程实例的名字叫MainThread,子线程的名字在创建时指定,我们用LoopThread命名子线程。名字仅仅在打印时用来显示,完全没有其他意义,如果不起名字Python就自动给线程命名为Thread-1,Thread-2……

Lock

多线程和多进程最大的不同在于,多进程中,同一个变量,各自有一份拷贝存在于每个进程中,互不影响,而多线程中,所有变量都由所有线程共享,所以,任何一个变量都可以被任何一个线程修改,因此,线程之间共享数据最大的危险在于多个线程同时改一个变量,把内容给改乱了。来看看多个线程同时操作一个变量怎么把内容给改乱了:

import time, threading

#假定这是你的银行存款:
balance = 0

def change_it(n):
	#先存后取,结果应该为0:
	global balance
	balance = balance + n
	balance = balance - n

def run_thread(n):
	for i in range(100000):
		change_it(n)

t1 = threading.Thread(target=run_thread, args=(5,))
t2 = threading.Thread(target=run_thread, args=(8,))
t1.start()
t2.start()
t1.join()
t2.join()
print(balance)

结果跑出来会不一样,可以给线程加lock解决问题 因为这个现在还不怎么用到 所以就不深究了 可以在这里查看

http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143192823818768cd506abbc94eb5916192364506fa5d000