2007年12月26日

python是支持多线程的,并且是native的线程。主要是通过thread和threading这两个模块来实现的。thread是比较底层的模 块,threading是对thread做了一些包装的,可以更加方便的被使用。这里需要提一下的是python对线程的支持还不够完善,不能利用多 CPU,但是下个版本的python中已经考虑改进这点,让我们拭目以待吧。
    threading模块里面主要是对一些线程的操作对象化了,创建了叫Thread的class。一般来说,使用线程有两种模式,一种是创建线程要执行的 函数,把这个函数传递进Thread对象里,让它来执行;另一种是直接从Thread继承,创建一个新的class,把线程执行的代码放到这个新的 class里。我们来看看这两种做法吧。

#-*- encoding: gb2312 -*-
import string, threading, time

def thread_main(a):
    
global count, mutex
    
# 获得线程名
    threadname = threading.currentThread().getName()
    
    
for x in xrange(0, int(a)):
        
# 取得锁
        mutex.acquire()
        count 
= count + 1
        
# 释放锁
        mutex.release()
        
print threadname, x, count
        time.sleep(
1)
    
def main(num):
    
global count, mutex
    threads 
= []
    
    count 
= 1
    
# 创建一个锁
    mutex = threading.Lock()
    
# 先创建线程对象
    for x in xrange(0, num):
        threads.append(threading.Thread(target
=thread_main, args=(10,)))
    
# 启动所有线程
    for t in threads:
        t.start()
    
# 主线程中等待所有子线程退出
    for t in threads:
        t.join()  
    
    
if __name__ == '__main__':
    num 
= 4
    
# 创建4个线程
    main(4)

上面的就是第一种做法,这种做法是很常见的,下面是另一种,曾经使用过Java的朋友应该很熟悉这种模式:

#-*- encoding: gb2312 -*-
import threading
import time

class Test(threading.Thread):
    
def __init__(self, num):
        threading.Thread.
__init__(self)
        self._run_num 
= num
    
    
def run(self):
        
global count, mutex
        threadname 
= threading.currentThread().getName()
    
        
for x in xrange(0, int(self._run_num)):
            mutex.acquire()
            count 
= count + 1
            mutex.release()
            
print threadname, x, count
            time.sleep(
1)

if __name__ == '__main__':
    
global count, mutex
    threads 
= []
    num 
= 4
    count 
= 1
    
# 创建锁
    mutex = threading.Lock()
    
# 创建线程对象
    for x in xrange(0, num):
        threads.append(Test(
10))
    
# 启动线程
    for t in threads:
        t.start()
    
# 等待子线程结束
    for t in threads:
        t.join() 

posted @ 2007-12-26 18:04 江边之鸟 阅读(815) | 评论 (1)编辑 收藏

     摘要:   阅读全文

posted @ 2007-12-26 17:54 江边之鸟 阅读(150) | 评论 (0)编辑 收藏

     摘要:   阅读全文

posted @ 2007-12-26 17:35 江边之鸟 阅读(252) | 评论 (0)编辑 收藏

     摘要:   阅读全文

posted @ 2007-12-26 17:34 江边之鸟 阅读(666) | 评论 (1)编辑 收藏

     摘要:   阅读全文

posted @ 2007-12-26 09:17 江边之鸟 阅读(199) | 评论 (0)编辑 收藏

2007年12月25日

如果我们想让系统启动的时候就执行某个程序,windows系统和unix系统是不一样的,对于unix只需要将要执行的命令放到 rc.local中,系统重新启动的时候就可以加载了。windows就麻烦多了,如果你将程序放到启动组中,只有输入了密码后,程序才被执行,如果想在 系统一启动的时候就执行程序,必须使用nt服务。
python下如何使用nt服务,其实很简单。
下载python的win32支持。我使用的是:pywin32-202.win32-py2.3.exe安装好后就可以来写我们的服务了。
我们先来建立一个空的服务,建立test1.py这个文件,并写入如下代码:

# -*- coding: cp936 -*-
import win32serviceutil
import win32service
import win32event

class test1(win32serviceutil.ServiceFramework):
    _svc_name_ = "test_python"
    _svc_display_name_ = "test_python"
    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)



        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    def SvcStop(self):
        # 先告诉SCM停止这个过程
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        # 设置事件
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        # 等待服务被停止
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

if __name__=='__main__':
    win32serviceutil.HandleCommandLine(test1)

这里注意,如果你需要更改文件名,比如将win32serviceutil.HandleCommandLine(test1)中的test1更改为你的文件名,同时class也需要和你的文件名一致,否则会出现服务不能启动的问题。
在命令窗口执行,test1.py可以看到帮助提示

C:\>test1.py
Usage: 'test1.py [options] install|update|remove|start [...]|stop|restart [...]|
debug [...]'
Options for 'install' and 'update' commands only:
 --username domain\username : The Username the service is to run under
 --password password : The password for the username
 --startup [manual|auto|disabled] : How the service starts, default = manual
 --interactive : Allow the service to interact with the desktop.

C:\>

安装我们的服务

[code:1:05b7353f1c]C:\>test1.py install
Installing service test_python to Python class C:\test1.test1
Service installed

C:\>

我们就可以用命令或者在控制面板-》管理工具-》服务中管理我们的服务了。在服务里面可以看到test_python这个服务,虽然这个服务什么都不做,但能启动和停止他。

posted @ 2007-12-25 15:12 江边之鸟 阅读(460) | 评论 (0)编辑 收藏

     摘要:   阅读全文

posted @ 2007-12-25 15:06 江边之鸟 阅读(256) | 评论 (0)编辑 收藏

     摘要:   阅读全文

posted @ 2007-12-25 13:57 江边之鸟 阅读(190) | 评论 (0)编辑 收藏

2007年12月24日

     摘要:   阅读全文

posted @ 2007-12-24 16:55 江边之鸟 阅读(188) | 评论 (0)编辑 收藏

     摘要:   阅读全文

posted @ 2007-12-24 14:58 江边之鸟 阅读(205) | 评论 (0)编辑 收藏

python有一个叫doctest的模块,很有意思,它可以让你在代码的注释中写入一些特殊的注释,
这些注释是测试程序。python可以自动对它进行测试。
import doctest

def myadd(a, b):
”’
>>> myadd(1, 2)
3
>>> myadd(4, 5)
9
”’
return a+b

if __name__ == ‘__main__’:
doctest.testmod()

输出:

D:\>e2.py -v
Trying:
myadd(1, 2)
Expecting:
3
ok
Trying:
myadd(4, 5)
Expecting:
9
ok
1 items had no tests:
__main__
1 items passed all tests:
2 tests in __main__.myadd
2 tests in 2 items.
2 passed and 0 failed.
Test passed.

注意运行的时候后面加上 -v 的参数,在linux下运行的话,最好这样 python 程序.py -v

posted @ 2007-12-24 10:44 江边之鸟 阅读(307) | 评论 (0)编辑 收藏

pywinauto is a set of python modules to automate the Microsoft Windows GUI. At it’s simplest it allows you to send mouse and keyboard actions to windows dialogs and controls.

pywinauto是一些用于自动化测试微软Windows图形界面的模块的集合。它可以允许你很容易的发送鼠标、键盘动作给Windows的对话框和控件。

地址:http://www.openqa.org/pywinauto/

posted @ 2007-12-24 10:43 江边之鸟 阅读(216) | 评论 (0)编辑 收藏

CherryPy is a pythonic, object-oriented HTTP framework.

CherryPy allows developers to build web applications in much the same way they would build any other object-oriented Python program. This usually results in smaller source code developed in less time.

CherryPy is now more than three years old and it is has proven very fast and stable. It is being used in production by many sites, from the simplest ones to the most demanding ones.

Oh, and most importantly: CherryPy is fun to work with :-) Here's how easy it is to write "Hello World" in CherryPy 3:

import cherrypy

class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True

cherrypy.quickstart(HelloWorld())

posted @ 2007-12-24 10:39 江边之鸟 阅读(136) | 评论 (0)编辑 收藏

2007年6月1日

     摘要: 1.法律的概念 法律是指国家制定或认可的,反映统治阶级意志的,依靠国家强制力保证实施的,具有普遍约束性的行为规范的总和。 2. 法的本质 (1) 法律是统治阶级意志的表现(2) 法律是上升为国家意志的统治阶级的意志(3) 法律归根结底是由统治阶级的物质生活条件决定的 3.法的基本特征 (1) 法律是一种特殊的社会规范 (2)   法是由...  阅读全文

posted @ 2007-06-01 10:03 江边之鸟 阅读(191) | 评论 (0)编辑 收藏

     摘要: 法律的概念:是指国家制定或认可的,反映统治阶级意志的,依靠国家强制力保证实施的,具有普遍约束性的行为规范的总和。 法系类型:大陆法系和普通法系。     法律关系由内容、主体、客体三个要素构成。  法的本质(多选) (1) 法律是统治阶级意志的表现 (2) 法律是上升为国家意志的统治阶级的意志 (3) ...  阅读全文

posted @ 2007-06-01 10:02 江边之鸟 阅读(208) | 评论 (0)编辑 收藏

仅列出标题  下一页