您的位置:首页 > 其它

设置超时,处理cookie,多线程

2014-07-05 17:20 471 查看
import socket
import urllib
from urllib import request
import http.cookiejar
import threading
socket.setdefaulttimeout(10)
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

lock = threading.Lock()
def ThreadProc(n):
if lock.acquire():
print(n)
lock.release()
class ThreadClass(threading.Thread):
def __init__(self, n):
threading.Thread.__init__(self)
self.n = n
def run(self):
if lock.acquire():
print(self.n)
lock.release()
threads = []
for i in range(10):
threads.append(threading.Thread(target = ThreadProc, args = (i,)))
for i in threads:
i.start()
for i in threads:
i.join()
threads.clear()
for i in range(10):
threads.append(ThreadClass(i * 100))
for i in threads:
i.start()
for i in threads:
i.join()
threading.Semaphore(3)
def Fn(n):
print(n)
threading.Timer(1, Fn, (n + 1,)).start()
Fn(1)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: