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)
设置超时,处理cookie,多线程
最新推荐文章于 2019-08-24 20:38:48 发布