PythonÏ̱߳à³Ì£¨Èý£©Í¬²½¶ÓÁÐ
ÎÒÃǾ³£»á²ÉÓÃÉú²úÕß/Ïû·ÑÕß¹ØÏµµÄÁ½¸öÏß³ÌÀ´´¦ÀíÒ»¸ö¹²Ïí»º³åÇøµÄÊý¾Ý¡£ÀýÈçÒ»¸öÉú²úÕßÏ߳̽ÓÊÜÓû§Êý¾Ý·ÅÈëÒ»¸ö¹²Ïí»º³åÇøÀµÈ´ýÒ»¸öÏû·ÑÕßÏ̶߳ÔÊý¾ÝÈ¡³ö´¦Àí¡£µ«ÊÇÈç¹û»º³åÇøµÄ̫С¶øÉú²úÕߺÍÏû·ÑÕßÁ½¸öÒì²½Ï̵߳ÄËٶȲ»Í¬Ê±£¬ÈÝÒ׳öÏÖÒ»¸öÏ̵߳ȴýÁíÒ»¸öÇé¿ö¡£ÎªÁ˾¡¿ÉÄܵÄËõ¶Ì¹²Ïí×ÊÔ´²¢ÒÔÏàͬËٶȹ¤×÷µÄ¸÷Ï̵߳ĵȴýʱ¼ä£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒ»¸ö“¶ÓÁДÀ´Ìṩ¶îÍâµÄ»º³åÇø¡£
´´½¨Ò»¸ö“¶ÓÁД¶ÔÏó
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()·½·¨´Ó¶Óͷɾ³ý²¢·µ»ØÒ»¸öÏîÄ¿¡£¿ÉÑ¡²ÎÊýΪblock£¬Ä¬ÈÏΪ1¡£Èç¹û¶ÓÁÐΪ¿ÕÇÒblockΪ1£¬get()¾Íʹµ÷ÓÃÏß³ÌÔÝÍ££¬Ö±ÖÁÓÐÏîÄ¿¿ÉÓá£Èç¹ûblockΪ0£¬¶ÓÁн«Òý·¢EmptyÒì³£¡£
ÎÒÃÇÓÃÒ»¸öÀý×ÓÀ´Õ¹Ê¾ÈçºÎʹÓÃQueue
# queue_example.py
from Queue import Queue
import threading
import random
import time
# Producer thread
class Producer(threading.Thread):
def __init__(self, threadname, queue):
threading.Thread.__init__(self, name = threadname)
self.sharedata = queue
def run(self):
for i in range(20):
print self.getName(),'adding',i,'to queue'
self.sharedata.put(i)
time.sleep(random.randrange(10)/10.0)
print self.getName(),'Finished'
# Consumer thread
class Consumer(threading.Thread):
def __init__(self, threadname, queue):
threading.Thread.__init__(self, name = threadname)
self.sharedata = queue
def run(self):
for i in range(20):
print self.getName(),'got a value:',self.sharedata.get()
time.sleep(random.randrange(10)/10.0)
print self.getName(),'Finished'
# Main thread
def main():
queue = Queue()
producer = Producer('Producer', queue)
consumer =
Ïà¹ØÎĵµ£º
ÕÒÁ˰ëÌìûÕÒ×Å£¬ÖÕÓÚÔÚÓ¢ÎÄÕ¾µãÉÏÕÒµ½£¬»¹ÓиÐлȺÀïµÄʯͷºÍÇòÃÔ
>>> s = datetime.datetime(2009,1,1)
>>> time.mktime(s.timetuple())
1230739200.0
±ðÍ⸶һ¸öpython¶Ôʱ¼äµÄһЩº¯Êý£¬ºÜºÃÓõÄ
ÎÒÃÇÏȵ¼Èë±ØÐëÓõ½µÄÒ»¸ömodule
>>> import time
ÉèÖÃÒ»¸öʱ¼äµÄ¸ñʽ£¬ÏÂÃæ»áÓõ½
& ......
from http://blog.alexa-pro.cn/?p=315
´ËÎĵµÊ¹ÓÃÆ½Ì¨Îª cPAMIE Build 2.0,ºÍ֮ǰµÄ°æ±¾ÓÐÃ÷ÏԵIJî±ð,¾ßÌå¿ÉÖ±½Ó¿´cPAMIE.py Ô´Âë
ÏÂÃæÊÇһЩ³£Óõķ½·¨
ie.navigate('http://blog.alexa.cn') ÓÃÀ´·ÃÎÊÒ»¸öÁ´½Ó¡£
ie.linkClick('linkname') ´ò¿ªÕâ¸öÒ³ÃæÖеÄÒ»¸öÁ¬½Ó ²ÎÊý: name»ò id
ie.textBoxSet('labels','python ......
server:
import sys
import socket
host = sys.argv[1]
port = int(sys.argv[2])
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
while 1:
try:
&n ......
from http://stackoverflow.com/questions/101268/hidden-features-of-python
http://www.okpython.com/bbs/thread-3434-1-1.html
http://www.okpython.com/bbs/thread-3436-1-2.html
http://www.okpython.com/bbs/thread-3437-1-2.html
http://www.okpython.com/bbs/thread-3438-1-1.html
http://www.okpython.com/bb ......
OS¿âÌṩÁËʹÓø÷ÖÖ²Ù×÷ϵͳ¹¦ÄܵĽӿڡ£
os.name ·µ»Øµ±Ç°²Ù×÷ϵͳÃû£¬¶¨ÒåÁË'posix'£¬'nt'£¬'mac'£¬'os2'£¬'ce'£¬'java'£¨ÎÒʹÓÃwin7/python3.1,Êä³öµÄÊÇnt¡£posix²»ÖªµÀÊÇʲôϵͳ£¬macÓ¦¸ÃÊÇApple»ú°É£¬os2Ò²²»ÖªµÀÊÇʲô¶«¶«£¬ceÓ¦¸ÃÊÇwinCE°É£¬ÕâÑùµÄ»°£¬pythonÓ¦¸ÃÖ§³ÖÏòCEÒÆÖ²°¡£¬Å¼×Ô¼ºÊÔ¹ý£¬Ã»±àÒë³É¹¦£¬java¹À¼ ......