Öð²½Ñݽø
f=open('/etc/motd','r')
longest=0
while True:
lineLen=len(f.readline().strip())
if not lineLen: break
if lineLen > longest:
longest=lineLen
f.close()
return longest
ÎÊÌ⣺һֱռÓÐÎļþ¾ä±úδÊÍ·Å
f=open('/etc/motd','r')
longest=0
allLines=f.readLines()
f.close()
for line in allLines:
lineLen=len(line.strip())
if lineLen > longest:
longest=lineLen
return longest
ÎÊÌ⣺¶ÁÍêÎļþËùÓÐÐÐÔÙ¿ªÊ¼¼ÆË㣬ºÄ·Ñ´óÁ¿ÄÚ´æ
f=open('/etc/motd','r')
allLineLens=[len(x.strip()) for x in f]
f.close()
return max(allLineLens)
ÎÊÌ⣺Îļþµü´úÆ÷Ò»ÐÐÒ»Ðеü´úf£¬Áбí½âÎöÐèÒªÎļþËùÓÐÐж¼»á¶Áµ½ÄÚ´æÖÐ
f=open('/etc/motd','r')
longest=max(len(x.strip()) for x in f)
f.close()
return longest
ÎÊÌ⣺¿ÉÈ¥µôÎļþ´ò¿ªÄ£Ê½£¬ÈÃpythonÈ¥´¦Àí´ò¿ªµÄÎļþ
return max(len(x.strip()) for x in open('etc/motd'))
Õâ ......
Ref : http://www.swig.org/translations/chinese/tutorial.html
¼ÙÉèÄãÓÐһЩcÄãÏëÔÙ¼ÓPython.¡£¾ÙÀýÀ´ËµÓÐÕâôһ¸öÎļþexample.c
/* File : example.c */
#include <time.h>
double My_variable = 3.0;
int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}
int my_mod(int x, int y) {
return (x%y);
}
char *get_time()
{
time_t ltime;
time(<ime);
return ctime(<ime);
}
½Ó¿ÚÎļþ
ÏÖÔÚ£¬ÎªÁËÔö¼ÓÕâЩÎļþµ½Äãϲ»¶µÄÓïÑÔÖУ¬ÄãÐèҪдһ¸ö½Ó¿ÚÎļþ£¨interface file£©Í¶Èëµ½swigÖС£ÕâЩC functionsµÄ½Ó¿ÚÎļþ¿ÉÄÜÈçÏÂËùʾ£º
/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
  ......
Ref : http://www.swig.org/translations/chinese/tutorial.html
¼ÙÉèÄãÓÐһЩcÄãÏëÔÙ¼ÓPython.¡£¾ÙÀýÀ´ËµÓÐÕâôһ¸öÎļþexample.c
/* File : example.c */
#include <time.h>
double My_variable = 3.0;
int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}
int my_mod(int x, int y) {
return (x%y);
}
char *get_time()
{
time_t ltime;
time(<ime);
return ctime(<ime);
}
½Ó¿ÚÎļþ
ÏÖÔÚ£¬ÎªÁËÔö¼ÓÕâЩÎļþµ½Äãϲ»¶µÄÓïÑÔÖУ¬ÄãÐèҪдһ¸ö½Ó¿ÚÎļþ£¨interface file£©Í¶Èëµ½swigÖС£ÕâЩC functionsµÄ½Ó¿ÚÎļþ¿ÉÄÜÈçÏÂËùʾ£º
/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
  ......
±³¾°
ÏîÄ¿µÄ
×Ô¶¯»¯²âÊÔÖÐÒѾʹÓÃÁË»ùÓÚPython
½Å±¾µÄ¿ò¼Ü£¬×Ô¶¯»¯¹ý³ÌÖÐ×î¹Ø¼üµÄÎÊÌâ¾ÍÊÇÈçºÎʵÏÖ׮ģ¿é¡£ÔËÓÃ
Python
Ç¿´óµÄ¹¦ÄÜ£¬ÊµÏÖÈκÎ׮ģ¿é¶¼ÊÇ¿ÉÄܵģ¬µ«ÊÇÊÇ·ñ±ØÐëÍêȫʹÓÃ
Python
ʵÏÖÄ£¿éÂß¼£¬³É±¾ÊÇÒ»¸ö¾ö¶¨ÐÔÒòËØ¡£ÔÚ׮ģ¿éÂß¼¼òµ¥µÄÇé¿öÏ£¬Ê¹ÓÃ
Python
Ä£ÄâÄ£¿éÂß¼²»µ«Ê¹×Ô¶¯»¯²âÊԵĽṹÇåÎú£¬Ò²¾ßÓиüºÃµÄÁé»îÐÔ£¬µ«ÊÇÈç¹û׮ģ¿éÂß¼¸´ÔÓ£¬ÊµÏÖÆðÀ´¿ÉÄÜÒªºÄ·ÑºÜ´óµÄ³É±¾£¬Ò²ÈÝÒ×ÓÉÓÚ׮ģ¿éÂß¼Óëʵ¼Ê²»·ûµ¼Ö²âÊÔ½á¹û²»¿ÉÐÅ¡£ÔÚÕâÖÖÇé¿öÏ£¬Èç¹ûÄܹ»½èÓÃ
RD
¿ª·¢µÄijЩ´úÂë¶Î
/
¿â£¬½«»á¶Ô²âÊÔ×Ô¶¯»¯´øÀ´ºÜ¶àЧÒæ¡£
ÁíÍ⣬ÔÚPython
Öе÷ÓÃ
C/C++
´úÂëµÄ·½·¨Ò²¿ÉÄÜÓ¦ÓÃÓÚ
C/C++
¿âµÄ²âÊÔÖУ¬ÕâÖÖ²âÊÔ·½·¨µÄ¿ÉÐÐÐÔ»¹ÓдýÑо¿¡£
ÒÔÏÂ×ܽá³ö¼¸ÖÖÔÚPython
Öе÷ÓÃ
C/C++
´úÂëµÄ·½·¨
ʹÓÃctypes
Ä£¿éµ÷ÓÃ
C
¶¯Ì¬¿â
´ÓPython2.5
¿ªÊ¼£¬
Python
¿ªÊ¼Ìṩ
ctypes
Ä£¿éÀ´Ìṩ¶Ô
C
ÓïÑÔ±àÒëµÄ¶¯Ì¬¿âÎļþµÄµ÷Óá£×¢Ò⣬
ÕâÀïÌØÖ¸C
µÄ¶¯Ì¬¿â
£¬ÓÃC++
±àÒëµÄ¶¯Ì¬¿â
ctypes
ËäÈ»Äܹ»¼ÓÔØ£¬µ«µ÷ÓÃʱµÄº¯ÊýÃûÒѾÓÉÓÚ
C++
µÄÖØÔØÌØÐÔ±»¼ÓÒÔÐ޸ģ¬ÄÑÒÔµ÷ÓᣠʹÓÃ
ctypes
µ÷ÓÃ
C
¶¯Ì¬¿âµÄº ......
±³¾°
ÏîÄ¿µÄ
×Ô¶¯»¯²âÊÔÖÐÒѾʹÓÃÁË»ùÓÚPython
½Å±¾µÄ¿ò¼Ü£¬×Ô¶¯»¯¹ý³ÌÖÐ×î¹Ø¼üµÄÎÊÌâ¾ÍÊÇÈçºÎʵÏÖ׮ģ¿é¡£ÔËÓÃ
Python
Ç¿´óµÄ¹¦ÄÜ£¬ÊµÏÖÈκÎ׮ģ¿é¶¼ÊÇ¿ÉÄܵģ¬µ«ÊÇÊÇ·ñ±ØÐëÍêȫʹÓÃ
Python
ʵÏÖÄ£¿éÂß¼£¬³É±¾ÊÇÒ»¸ö¾ö¶¨ÐÔÒòËØ¡£ÔÚ׮ģ¿éÂß¼¼òµ¥µÄÇé¿öÏ£¬Ê¹ÓÃ
Python
Ä£ÄâÄ£¿éÂß¼²»µ«Ê¹×Ô¶¯»¯²âÊԵĽṹÇåÎú£¬Ò²¾ßÓиüºÃµÄÁé»îÐÔ£¬µ«ÊÇÈç¹û׮ģ¿éÂß¼¸´ÔÓ£¬ÊµÏÖÆðÀ´¿ÉÄÜÒªºÄ·ÑºÜ´óµÄ³É±¾£¬Ò²ÈÝÒ×ÓÉÓÚ׮ģ¿éÂß¼Óëʵ¼Ê²»·ûµ¼Ö²âÊÔ½á¹û²»¿ÉÐÅ¡£ÔÚÕâÖÖÇé¿öÏ£¬Èç¹ûÄܹ»½èÓÃ
RD
¿ª·¢µÄijЩ´úÂë¶Î
/
¿â£¬½«»á¶Ô²âÊÔ×Ô¶¯»¯´øÀ´ºÜ¶àЧÒæ¡£
ÁíÍ⣬ÔÚPython
Öе÷ÓÃ
C/C++
´úÂëµÄ·½·¨Ò²¿ÉÄÜÓ¦ÓÃÓÚ
C/C++
¿âµÄ²âÊÔÖУ¬ÕâÖÖ²âÊÔ·½·¨µÄ¿ÉÐÐÐÔ»¹ÓдýÑо¿¡£
ÒÔÏÂ×ܽá³ö¼¸ÖÖÔÚPython
Öе÷ÓÃ
C/C++
´úÂëµÄ·½·¨
ʹÓÃctypes
Ä£¿éµ÷ÓÃ
C
¶¯Ì¬¿â
´ÓPython2.5
¿ªÊ¼£¬
Python
¿ªÊ¼Ìṩ
ctypes
Ä£¿éÀ´Ìṩ¶Ô
C
ÓïÑÔ±àÒëµÄ¶¯Ì¬¿âÎļþµÄµ÷Óá£×¢Ò⣬
ÕâÀïÌØÖ¸C
µÄ¶¯Ì¬¿â
£¬ÓÃC++
±àÒëµÄ¶¯Ì¬¿â
ctypes
ËäÈ»Äܹ»¼ÓÔØ£¬µ«µ÷ÓÃʱµÄº¯ÊýÃûÒѾÓÉÓÚ
C++
µÄÖØÔØÌØÐÔ±»¼ÓÒÔÐ޸ģ¬ÄÑÒÔµ÷ÓᣠʹÓÃ
ctypes
µ÷ÓÃ
C
¶¯Ì¬¿âµÄº ......
±³¾°
ÏîÄ¿µÄ
×Ô¶¯»¯²âÊÔÖÐÒѾʹÓÃÁË»ùÓÚPython
½Å±¾µÄ¿ò¼Ü£¬×Ô¶¯»¯¹ý³ÌÖÐ×î¹Ø¼üµÄÎÊÌâ¾ÍÊÇÈçºÎʵÏÖ׮ģ¿é¡£ÔËÓÃ
Python
Ç¿´óµÄ¹¦ÄÜ£¬ÊµÏÖÈκÎ׮ģ¿é¶¼ÊÇ¿ÉÄܵģ¬µ«ÊÇÊÇ·ñ±ØÐëÍêȫʹÓÃ
Python
ʵÏÖÄ£¿éÂß¼£¬³É±¾ÊÇÒ»¸ö¾ö¶¨ÐÔÒòËØ¡£ÔÚ׮ģ¿éÂß¼¼òµ¥µÄÇé¿öÏ£¬Ê¹ÓÃ
Python
Ä£ÄâÄ£¿éÂß¼²»µ«Ê¹×Ô¶¯»¯²âÊԵĽṹÇåÎú£¬Ò²¾ßÓиüºÃµÄÁé»îÐÔ£¬µ«ÊÇÈç¹û׮ģ¿éÂß¼¸´ÔÓ£¬ÊµÏÖÆðÀ´¿ÉÄÜÒªºÄ·ÑºÜ´óµÄ³É±¾£¬Ò²ÈÝÒ×ÓÉÓÚ׮ģ¿éÂß¼Óëʵ¼Ê²»·ûµ¼Ö²âÊÔ½á¹û²»¿ÉÐÅ¡£ÔÚÕâÖÖÇé¿öÏ£¬Èç¹ûÄܹ»½èÓÃ
RD
¿ª·¢µÄijЩ´úÂë¶Î
/
¿â£¬½«»á¶Ô²âÊÔ×Ô¶¯»¯´øÀ´ºÜ¶àЧÒæ¡£
ÁíÍ⣬ÔÚPython
Öе÷ÓÃ
C/C++
´úÂëµÄ·½·¨Ò²¿ÉÄÜÓ¦ÓÃÓÚ
C/C++
¿âµÄ²âÊÔÖУ¬ÕâÖÖ²âÊÔ·½·¨µÄ¿ÉÐÐÐÔ»¹ÓдýÑо¿¡£
ÒÔÏÂ×ܽá³ö¼¸ÖÖÔÚPython
Öе÷ÓÃ
C/C++
´úÂëµÄ·½·¨
ʹÓÃctypes
Ä£¿éµ÷ÓÃ
C
¶¯Ì¬¿â
´ÓPython2.5
¿ªÊ¼£¬
Python
¿ªÊ¼Ìṩ
ctypes
Ä£¿éÀ´Ìṩ¶Ô
C
ÓïÑÔ±àÒëµÄ¶¯Ì¬¿âÎļþµÄµ÷Óá£×¢Ò⣬
ÕâÀïÌØÖ¸C
µÄ¶¯Ì¬¿â
£¬ÓÃC++
±àÒëµÄ¶¯Ì¬¿â
ctypes
ËäÈ»Äܹ»¼ÓÔØ£¬µ«µ÷ÓÃʱµÄº¯ÊýÃûÒѾÓÉÓÚ
C++
µÄÖØÔØÌØÐÔ±»¼ÓÒÔÐ޸ģ¬ÄÑÒÔµ÷ÓᣠʹÓÃ
ctypes
µ÷ÓÃ
C
¶¯Ì¬¿âµÄº ......
ÎÒÃÇÔÚ×öÈí¼þ¿ª·¢µÄʱºòºÜ¶àÒªÓõ½¶àÏ̼߳¼Êõ¡£ÀýÈçÈç¹û×öÒ»¸öÏÂÔØÈí¼þÏóflashget¾ÍÒªÓõ½¡¢ÏóÔÚÏßÊÓƵ¹¤¾ßrealplayerÒ²ÒªÓõ½ÒòΪҪͬʱÏÂÔØmedia stream»¹Òª²¥·Å¡£ÆäʵÀý×ÓÊǺܶàµÄ¡£
Ïß³ÌÏà¶Ô½ø³ÌÀ´ËµÊÇ“ÇáÁ¿¼¶”µÄ£¬²Ù×÷ϵͳÓýÏÉÙµÄ×ÊÔ´´´½¨ºÍ¹ÜÀíÏ̡߳£³ÌÐòÖеÄÏß³ÌÔÚÏàͬµÄÄÚ´æ¿Õ¼äÖÐÖ´ÐУ¬²¢¹²ÏíÐí¶àÏàͬµÄ×ÊÔ´¡£
ÔÚpython
ÖÐÈçºÎ´´½¨Ò»¸öÏ̶߳ÔÏó
Èç¹ûÄãÒª´´½¨Ò»¸öÏ̶߳ÔÏ󣬺ܼòµ¥£¬Ö»ÒªÄãµÄÀà¼Ì³Ðthreading.Thread
£¬È»ºóÔÚ__init__
ÀïÊ×Ïȵ÷ÓÃthreading.Thread
µÄ__init__
·½·¨¼´¿É
import threading
class mythread(threading.Thread):
def __init__(self, threadname):
threading.Thread.__init__(self, name = threadname)
....
Õâ²Å½ö½öÊǸö¿ÕỊ̈߳¬Îҿɲ»ÊÇÒªËûÀ¿Õ³µµÄ£¬Ëû¿ÉµÃ¸øÎҸɵãʵÔڻºÜ¼òµ¥£¬ÖØдÀàµÄrun()
·½·¨¼´¿É£¬°ÑÄãÒªÔÚÏß³ÌÖ´ÐÐʱ×öµÄÊÂÇ鶼·Åµ½ÀïÃæ
import threading
import time
class mythread(threading.Thread):
def __init__(...):
....
def run(self):
for i in range(10):
print self.getName, i
time.sleep(1)
ÒÔÉÏ´úÂëÎÒÃÇÈÃÕâ¸öÏß³ÌÔÚÖ´ÐÐÖ®ºóÿ¸ô1ÃëÊä³öÒ»´ÎÐÅÏ¢µ½ÆÁÄ»£¬10´Îºó½áÊø
g ......
¶à¸öÖ´ÐÐÏ߳̾³£Òª¹²ÏíÊý¾Ý£¬Èç¹û½ö½ö¶ÁÈ¡¹²ÏíÊý¾Ý»¹ºÃ£¬µ«ÊÇÈç¹û¶à¸öÏß³ÌÒªÐ޸Ĺ²ÏíÊý¾ÝµÄ»°¾Í¿ÉÄܳöÏÖÎÞ·¨Ô¤ÁϵĽá¹û¡£
¼ÙÈçÁ½¸öÏ̶߳ÔÏót1
ºÍt2
¶¼Òª¶ÔÊýÖµnum=0
½øÐÐÔö1ÔËË㣬ÄÇôt1
ºÍt2
¶¼¸÷¶Ônum
ÐÞ¸Ä10
´ÎµÄ»°£¬ÄÇônum
×îÖյĽá¹ûÓ¦¸ÃΪ20
¡£µ«ÊÇÈç¹ûµ±t1
È¡µÃnum
µÄֵʱ£¨¼ÙÈç´Ëʱnum
Ϊ0
£©£¬ÏµÍ³°Ñt1
µ÷¶ÈΪ“sleeping
”״̬£¬¶ø´Ëʱt2
ת»»Îª“running
”״̬£¬´Ëʱt2
»ñµÃµÄnum
µÄֵҲΪ0
£¬È»ºóËû°Ñnum+1
µÄÖµ1
¸³¸ønum
¡£ÏµÍ³ÓÖ°Ñt2
ת»¯Îª“sleeping
”״̬£¬t1
Ϊ“running
”״̬£¬ÓÉÓÚt1
ÒѾµÃµ½num
ֵΪ0
£¬ËùÒÔËûÒ²°Ñnum+1
µÄÖµ¸³¸øÁËnum
Ϊ1
¡£±¾À´ÊÇ2
´ÎÔö1
ÔËÐУ¬½á¹ûÈ´ÊÇnum
Ö»ÔöÁË1
´Î¡£ÀàËÆÕâÑùµÄÇé¿öÔÚ¶àÏß³ÌͬʱִÐеÄʱºòÊÇÓпÉÄÜ·¢ÉúµÄ¡£ËùÒÔΪÁË·ÀÖ¹ÕâÀàÇé¿öµÄ³öÏÖ¾ÍҪʹÓÃÏß³Ìͬ²½»úÖÆ¡£
×î¼òµ¥µÄͬ²½»úÖƾÍÊÇ“Ëø”
Ëø¶ÔÏóÓÃthreading.RLock
Àà´´½¨
mylock = threading.RLock()
ÈçºÎʹÓÃËøÀ´Í¬²½Ïß³ÌÄØ£¿Ï߳̿ÉÒÔʹÓÃËøµÄacquire()
......
ÎÒÃǾ³£»á²ÉÓÃÉú²úÕß/Ïû·ÑÕß¹ØϵµÄÁ½¸öÏß³ÌÀ´´¦ÀíÒ»¸ö¹²Ïí»º³åÇøµÄÊý¾Ý¡£ÀýÈçÒ»¸öÉú²úÕßÏ߳̽ÓÊÜÓû§Êý¾Ý·ÅÈëÒ»¸ö¹²Ïí»º³åÇøÀµÈ´ýÒ»¸öÏû·ÑÕßÏß
³Ì¶ÔÊý¾ÝÈ¡³ö´¦Àí¡£µ«ÊÇÈç¹û»º³åÇøµÄ̫С¶øÉú²úÕߺÍÏû·ÑÕßÁ½¸öÒì²½Ï̵߳ÄËٶȲ»Í¬Ê±£¬ÈÝÒ׳öÏÖÒ»¸öÏ̵߳ȴýÁíÒ»¸öÇé¿ö¡£ÎªÁ˾¡¿ÉÄܵÄËõ¶Ì¹²Ïí×ÊÔ´²¢ÒÔÏàͬ
Ëٶȹ¤×÷µÄ¸÷Ï̵߳ĵȴýʱ¼ä£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒ»¸ö“¶ÓÁДÀ´Ìṩ¶îÍâµÄ»º³åÇø¡£
´´½¨Ò»¸ö“¶ÓÁД¶ÔÏó
import Queue
myqueue = Queue.Queue(maxsize = 10)
Queue.Queue
À༴ÊÇÒ»¸ö¶ÓÁеÄͬ²½ÊµÏÖ¡£¶ÓÁг¤¶È¿ÉΪÎÞÏÞ»òÕßÓÐÏÞ¡£¿Éͨ¹ýQueue
µÄ¹¹Ô캯ÊýµÄ¿ÉÑ¡²ÎÊýmaxsize
À´É趨¶ÓÁг¤¶È¡£Èç¹ûmaxsize
СÓÚ1
¾Í±íʾ¶ÓÁ㤶ÈÎÞÏÞ¡£
½«Ò»¸öÖµ·ÅÈë¶ÓÁÐÖÐ
myqueue.put(10)
µ÷ÓöÓÁжÔÏóµÄput()
·½·¨ÔÚ¶Óβ²åÈëÒ»¸öÏîÄ¿¡£put()
ÓÐÁ½¸ö²ÎÊý£¬µÚÒ»¸öitem
Ϊ±ØÐèµÄ£¬Îª²åÈëÏîÄ¿µÄÖµ£»µÚ¶þ¸öblock
Ϊ¿ÉÑ¡²ÎÊý£¬Ä¬ÈÏΪ1
¡£Èç¹û¶ÓÁе±Ç°Îª¿ÕÇÒblock
Ϊ1
£¬put()
·½·¨¾Íʹµ÷ÓÃÏß³ÌÔÝÍ£,Ö±µ½¿Õ³öÒ»¸öÊý¾Ýµ¥Ôª¡£Èç¹ûblock
Ϊ0
£¬put
·½·¨½«Òý·¢Full
Òì³£¡£
½«Ò»¸öÖµ´Ó¶ÓÁÐÖÐÈ¡³ö
myqueue.get()
µ÷ÓöÓÁжÔÏóµÄget()
·½·¨´Ó¶Óͷɾ³ý²¢·µ»ØÒ ......