#! /usr/bin/python
#-* coding: utf-8 -*
# __author__ ="tyomcat"
import threading
import time
import os
def booth(tid):
global i
global lock
while True:
lock.acquire()
if i!=0:
i=i-1
print "窗口:",tid,",剩余票数:",i
time.sleep(1)
else:
print "Thread_id",tid,"No more tickets"
os._exit(0)
lock.release()
time.sleep(1)
i = 100
lock=threading.Lock()
for k in range(10):
new_thread = threading.Thread(target=booth,args=(k,))
new_thread.start()
#! /usr/bin/python
#-* coding: utf-8 -*
# __author__ ="tyomcat"
import time
import sys
# 生产者
def produce(l):
i=0
while 1:
if i < 10:
l.append(i)
yield i
i=i+1
time.sleep(1)
else:
return
# 消费者
def consume(l):
p = produce(l)
while 1:
try:
p.next()
while len(l) > 0:
print l.pop()
except StopIteration:
sys.exit(0)
if __name__ == "__main__":
l = []
consume(l)
>>>import subprocess >>>command_line=raw_input() ping -c 10 www.baidu.com >>>args=shlex.split(command_line) >>>p=subprocess.Popen(args)
import subprocess child1 = subprocess.Popen(["ls","-l"], stdout=subprocess.PIPE) child2 = subprocess.Popen(["wc"], stdin=child1.stdout,stdout=subprocess.PIPE) out = child2.communicate() print(out)
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ == "tyomcat"
# "我的电脑有4个cpu"
from multiprocessing import Pool
import os, time
def long_time_task(name):
print 'Run task %s (%s)...' % (name, os.getpid())
start = time.time()
time.sleep(3)
end = time.time()
print 'Task %s runs %0.2f seconds.' % (name, (end - start))
if __name__=='__main__':
print 'Parent process %s.' % os.getpid()
p = Pool()
for i in range(4):
p.apply_async(long_time_task, args=(i,))
print 'Waiting for all subprocesses done...'
p.close()
p.join()
print 'All subprocesses done.'
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ == "tyomcat"
from multiprocessing import Queue,Pool
import multiprocessing,time,random
def write(q):
for value in ['A','B','C','D']:
print "Put %s to Queue!" % value
q.put(value)
time.sleep(random.random())
def read(q,lock):
while True:
lock.acquire()
if not q.empty():
value=q.get(True)
print "Get %s from Queue" % value
time.sleep(random.random())
else:
break
lock.release()
if __name__ == "__main__":
manager=multiprocessing.Manager()
q=manager.Queue()
p=Pool()
lock=manager.Lock()
pw=p.apply_async(write,args=(q,))
pr=p.apply_async(read,args=(q,lock))
p.close()
p.join()
print
print "所有数据都写入并且读完"
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有