-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
罗晟
committed
Feb 14, 2015
1 parent
c15c8f8
commit 1bf4683
Showing
43 changed files
with
947 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
echo $PATH #环境引用的先后 | ||
/usr/local/bin/python | ||
/usr/bin/python | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Created by iFantastic on 15-2-12 | ||
__author__ = 'cluo' | ||
|
||
import hashlib | ||
h = hashlib.md5() | ||
h.update('cluo') | ||
print h.hexdigest() #字符 | ||
print h.digest() #二进制 | ||
|
||
|
||
h = hashlib.sha1() | ||
|
||
|
||
|
||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Created by iFantastic on 15-2-12 | ||
__author__ = 'cluo' | ||
#获取注册程序 | ||
#每个进程都用一张表来 保存信号量 | ||
#注册函数为SIG_IGN 忽略 | ||
#注册函数为SIG_DEF 默认行为 | ||
#信号是进程间通信 模拟硬件通信的行为 | ||
|
||
import signal | ||
|
||
def alarm_received(n, stack): | ||
return | ||
|
||
signal.signal(signal.SIGALRM, alarm_received) | ||
|
||
signals_to_names = dict( | ||
(getattr(signal, n), n) | ||
for n in dir(signal) | ||
if n.startswith('SIG') and '_' not in n | ||
) | ||
|
||
for s, name in sorted(signals_to_names.items()): | ||
handler = signal.getsignal(s) #获取注册信号的函数 | ||
if handler is signal.SIG_DFL: | ||
handler = 'SIG_DFL' | ||
elif handler is signal.SIG_IGN: | ||
handler = 'SIG_IGN' | ||
print '%-10s (%2d):' % (name, s), handler | ||
|
||
# if __name__ == '__main__': | ||
# print signals_to_names | ||
# print dir(signal) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Created by iFantastic on 15-2-12 | ||
__author__ = 'cluo' | ||
import signal | ||
import os | ||
import time | ||
if __name__ == '__main__': | ||
def do_exit(sig, stack): | ||
raise SystemExit('Exiting') | ||
signal.signal(signal.SIGINT, signal.SIG_IGN) #ctrl + c SIGINT 中断程序 处理函数为 signal.SIG_IGN 时忽略信号 | ||
signal.signal(signal.SIGUSR1, do_exit) #退出程序 | ||
print 'My PID:', os.getpid() | ||
signal.pause() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Created by iFantastic on 15-2-12 | ||
__author__ = 'cluo' | ||
|
||
import signal | ||
import os | ||
|
||
|
||
|
||
# Define signal handler function | ||
def myHandler(signum, frame): | ||
print signum | ||
print os.getpid() | ||
print signal.SIGALRM #14定时唤醒 | ||
print("Now, it's the time") | ||
os.kill(os.getpid(),signal.SIGALRM) | ||
exit() | ||
|
||
# register signal.SIGALRM's handler | ||
signal.signal(signal.SIGALRM, myHandler) | ||
signal.alarm(2) | ||
signal.pause() | ||
print('End of Signal Demo') | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Created by iFantastic on 15-2-12 | ||
__author__ = 'cluo' | ||
import os | ||
import signal | ||
#handler为signal.SIG_IGN时,信号被无视(ignore) | ||
#当handler为singal.SIG_DFL,进程采取默认操作(default)。 | ||
# 当handler为一个函数名时,进程采取函数中定义的操作。 | ||
#我们首先使用signal.signal()函数来预设信号处理函数。 | ||
# 然后我们执行signal.pause()来让该进程暂停以等待信号, | ||
#以等待信号。当信号SIGUSR1被传递给该进程时,进程从暂停中 | ||
#并根据预设,执行SIGTSTP的信号处理函数myHandler()。 | ||
#myHandler的两个参数一个用来识别信号(signum), | ||
#另一个用来获得信号发生时,进程栈的状况(stack frame)。 | ||
#这两个参数都是由signal.singnal()函数来传递的。 | ||
|
||
# 通过按下CTRL+Z向该进程发送SIGTSTP信号。 | ||
# 我们可以看到,进程执行了myHandle()函数, | ||
# 随后返回主程序,继续执行。(当然,也可以用$ps查询process ID, 再使用$kill来发出信号。) | ||
# (进程并不一定要使用signal.pause()暂停以等待信号, | ||
# 它也可以在进行工作中接受信号, | ||
# 比如将上面的signal.pause()改为一个需要长时间工作的循环。 | ||
# while true | ||
# doing | ||
# | ||
# ) | ||
|
||
|
||
def sigterm_clean(signum, frame): | ||
try: | ||
print 'alive' | ||
print 'over' | ||
print signal.SIGTERM #15 等待15信号 | ||
os.kill(os.getpid(),15) #相当于 kill -15 pid | ||
except OSError: | ||
print 'error' | ||
pass | ||
|
||
signal.signal(signal.SIGTERM, sigterm_clean) | ||
print os.getpid() | ||
|
||
signal.pause() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Created by iFantastic on 15-2-12 | ||
import signal | ||
import time | ||
#定时触发信号 | ||
__author__ = 'cluo' | ||
def receive_alarm(signum, stack): | ||
print 'Alarm :',time.ctime() | ||
signal.signal(signal.SIGALRM, receive_alarm) | ||
signal.alarm(2) | ||
print 'Before:',time.ctime() | ||
time.sleep(4) | ||
print 'After :',time.ctime() | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Created by iFantastic on 15-2-12 | ||
__author__ = 'cluo' | ||
import signal | ||
import os | ||
import time | ||
|
||
#信号(signal)是Linux进程间通信的一种机制,全称为软中断信号,也被称为软中断。信号本质上是在软件层次上对硬件中断机制的一种模拟。 | ||
def receive_signal(signum, stack): | ||
print '==============================received:',signum | ||
signal.signal(signal.SIGUSR1, receive_signal) | ||
signal.signal(signal.SIGUSR2, receive_signal) | ||
|
||
#kill -USR1 $pid | ||
#kill -USR1 $pid | ||
#kill -INT $pid ctrl + c | ||
#kill -QUIT $pid crtl + d | ||
|
||
|
||
if __name__ == '__main__': | ||
print 'my pid is :', os.getpid() | ||
while True: | ||
print 'Waiting...' | ||
print os.getpid() | ||
print 'usr1:',signal.SIGUSR1 | ||
print 'usr2:',signal.SIGUSR2 | ||
time.sleep(20) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Created by iFantastic on 15-2-12 | ||
__author__ = 'cluo' | ||
import signal | ||
import threading | ||
import os | ||
import time | ||
|
||
#信号一般是线程级别的通信, | ||
#子线程是接不了信号的,子线程pause, 子线程将永远不会退出 | ||
#只有主线程才能接受闹铃 | ||
|
||
if __name__ == '__main__': | ||
#主线程 | ||
def signal_handler(num, stack): | ||
print 'Received signal %d in %s' %(num, threading.currentThread().name) | ||
signal.signal(signal.SIGUSR1, signal_handler) #python 中的信号处理函数 | ||
|
||
|
||
def signal_handler_1(num, stack): | ||
print 'Received signal %d in %s' %(num, threading.currentThread().name) | ||
signal.signal(signal.SIGTERM, signal_handler_1) #python 中的信号处理函数 | ||
|
||
|
||
|
||
|
||
#收信号线程 | ||
def wait_for_signal(): | ||
print 'Waiting for signal in',threading.currentThread().name | ||
signal.pause() #线程 接受不了任何信号 | ||
print 'Done waiting' | ||
|
||
receiver = threading.Thread(target=wait_for_signal,name='receiver') | ||
receiver.start() | ||
time.sleep(0.1) | ||
|
||
#发信号线程 | ||
def send_signal(): | ||
print 'Sending signal in',threading.currentThread().name | ||
os.kill(os.getpid(), signal.SIGUSR1) | ||
os.kill(os.getpid(), signal.SIGTERM) | ||
|
||
sender = threading.Thread(target=send_signal,name='sender') | ||
start = time.time() | ||
sender.start() | ||
sender.join() | ||
|
||
print 'Waiting for',receiver.name | ||
signal.alarm(2) #2秒后自己送送SIGALRM 终止程序(防止阻塞) | ||
print os.getpid() | ||
receiver.join() | ||
end = time.time() | ||
interval = end - start | ||
print interval | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Created by iFantastic on 15-2-12 | ||
__author__ = 'cluo' | ||
import signal | ||
import os | ||
# Define signal handler function | ||
def myHandler(signum, frame): | ||
print('I received: ', signum) | ||
print signal.SIGTSTP | ||
print os.getpid() | ||
print 'end' | ||
os.kill(os.getpid(),signal.SIGTERM) | ||
|
||
# register signal.SIGTSTP's handler | ||
signal.signal(signal.SIGTSTP, myHandler) | ||
while True: | ||
print('End of Signal Demo') | ||
|
||
#通过按下CTRL+Z向该进程发送SIGTSTP信号 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Created by iFantastic on 15-2-12 | ||
__author__ = 'cluo' | ||
#尽管线程中能设置闹铃 但总是主线程接受 | ||
import signal | ||
import time | ||
import threading | ||
|
||
def signal_handler(num, stack): | ||
print time.ctime(),'Alarm in ', threading.currentThread().name | ||
signal.signal(signal.SIGALRM, signal_handler) | ||
|
||
def use_alarm(): | ||
t_name = threading.currentThread().name | ||
print time.ctime(), 'Setting alarm in ',t_name | ||
signal.alarm(1) #子线程的闹铃,只主线程接收 | ||
#不会中断3秒的延迟时 | ||
print time.ctime(),'Sleeping in',t_name | ||
time.sleep(3) | ||
print time.ctime(),'Done with sleep in',t_name | ||
|
||
alarm_thread = threading.Thread(target=use_alarm,name='alarm_thread') | ||
alarm_thread.start() | ||
time.sleep(0.1) | ||
print time.ctime(),'Waiting for',alarm_thread.name | ||
alarm_thread.join() | ||
print time.ctime(),'Exting normally' |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
线程池原理及python实现 | ||
为什么需要线程池 | ||
目前的大多数网络服务器,包括Web服务器、Email服务器以及数据库服务器等都具有一个共同点,就是单位时间内必须处理数目巨大的连接请求,但处理时间却相对较短。 | ||
传统多线程方案中我们采用的服务器模型则是一旦接受到请求之后,即创建一个新的线程,由该线程执行任务。任务执行完毕后,线程退出,这就是是“即时创建, 即时销毁”的策略。尽管与创建进程相比,创建线程的时间已经大大的缩短,但是如果提交给线程的任务是执行时间较短,而且执行次数极其频繁,那么服务器将处于不停的创建线程,销毁线程的状态。 | ||
我们将传统方案中的线程执行过程分为三个过程:T1、T2、T3: | ||
T1:线程创建时间 | ||
T2:线程执行时间,包括线程的同步等时间 | ||
T3:线程销毁时间 | ||
|
||
那么我们可以看出,线程本身的开销所占的比例为(T1+T3) / (T1+T2+T3)。如果线程执行的时间很短的话,这比开销可能占到20%-50%左右。如果任务执行时间很频繁的话,这笔开销将是不可忽略的。 | ||
除此之外,线程池能够减少创建的线程个数。通常线程池所允许的并发线程是有上界的,如果同时需要并发的线程数超过上界,那么一部分线程将会等待。而传统方案中,如果同时请求数目为2000,那么最坏情况下,系统可能需要产生2000个线程。尽管这不是一个很大的数目,但是也有部分机器可能达不到这种要求。 | ||
因此线程池的出现正是着眼于减少线程池本身带来的开销。线程池采用预创建的技术,在应用程序启动之后,将立即创建一定数量的线程(N1),放入空闲队列 中。这些线程都是处于阻塞(Suspended)状态,不消耗CPU,但占用较小的内存空间。当任务到来后,缓冲池选择一个空闲线程,把任务传入此线程中运行。当N1个线程都在处理任务后,缓冲池自动创建一定数量的新线程,用于处理更多的任务。在任务执行完毕后线程也不退出,而是继续保持在池中等待下一次的任务。当系统比较空闲时,大部分线程都一直处于暂停状态,线程池自动销毁一部分线程,回收系统资源。 | ||
基于这种预创建技术,线程池将线程创建和销毁本身所带来的开销分摊到了各个具体的任务上,执行次数越多,每个任务所分担到的线程本身开销则越小,不过我们另外可能需要考虑进去线程之间同步所带来的开销。 | ||
|
||
构建线程池框架 | ||
|
||
一般线程池都必须具备下面几个组成部分: | ||
线程池管理器:用于创建并管理线程池 | ||
: 线程池中实际执行的线程 | ||
: 尽管线程池大多数情况下是用来支持网络服务器,但是我们将线程执行的任务抽象出来,形成任务接口,从而是的线程池与具体的任务无关。 | ||
:线程池的概念具体到实现则可能是队列,链表之类的数据结构,其中保存执行线程。 | ||
|
||
我们把任务放进队列中去,然后开N个线程,每个线程都去队列中取一个任务,执行完了之后告诉系统说我执行完了,然后接着去队列中取下一个任务,直至队列中所有任务取空,退出线程。 | ||
|
||
这就是一般的线程池实现的原理,下面看一个实际的代码: | ||
|
Oops, something went wrong.