Skip to content

Commit bdba0c6

Browse files
committed
By Zhao Kun Peng
1 parent e2c3e4e commit bdba0c6

File tree

15 files changed

+373
-0
lines changed

15 files changed

+373
-0
lines changed

LOG10/01.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import logging
2+
LOG_FORMAT = "%(asctime)s=====%(levelname)s+++++%(message)s"
3+
logging.basicConfig(filename="NJUST.txt",level=logging.DEBUG,format=LOG_FORMAT)
4+
logging.debug("This is a debug log.")
5+
logging.info("This is a info log.")
6+
logging.warning("This is a warning log.")
7+
logging.error("This is a error log.")
8+
logging.critical("This is a critical log.")
9+
logging.log(logging.DEBUG,"This is a debug log.")
10+
logging.log(logging.INFO, "This is a info log.")
11+
logging.log(logging.WARNING, "This is a warning log.")
12+
logging.log(logging.ERROR, "This is a error log.")
13+
logging.log(logging.CRITICAL, "This is a critical log.")

LOG10/02.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import logging
2+
a="veribule"
3+
LOG_FORMAT = "%(user)s[%(ip)s] - %(levelname)s***%(name)s******%(message)s***%(asctime)s"
4+
DATE_FORMAT = "%Y-%m-%d %I:%M:%S %P"
5+
logging.basicConfig(datefmt = DATE_FORMAT,format=LOG_FORMAT,level=logging.DEBUG,filemode='w',filename="/home/tlxy/dana/tutu.txt")
6+
7+
logging.debug("This is debug DE {}".format(a), exc_info=True,extra={'user':'tom','ip':'47.98.53.222'})
8+
logging.info("This is info",extra={'user':'tom','ip':'47.98.53.222'})
9+
logging.log(logging.WARNING,'This is warning',extra={'user':'tom','ip':'47.98.53.222'})
10+
logging.error('this is a error',extra={'user':'tom','ip':'47.98.53.222'})
11+
logging.log(logging.CRITICAL, "This is a Critical",extra={'user':'tom','ip':'47.98.53.222'})

LOG10/03.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import threading
2+
3+
sum = 0
4+
loopsum = 1000000
5+
6+
def myAdd():
7+
global sum, loopsum
8+
for i in range(1, loopsum):
9+
sum += 1
10+
11+
def myMinu():
12+
global sum, loopsum
13+
for i in range(1, loopsum):
14+
sum -= 1
15+
16+
if __name__ == '__main__':
17+
print('Starting ......{}'.format(sum))
18+
19+
t1 = threading.Thread(target=myAdd(), args=())
20+
t2 = threading.Thread(target=myMinu(), args=())
21+
t1.start()
22+
t2.start()
23+
24+
t1.join()
25+
t2.join()
26+
27+
print("Done ... ... {}".format(sum))

LOG10/04.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import threading
2+
sum = 0
3+
loopsum = 100000
4+
5+
lock = threading.Lock()
6+
7+
def myadd():
8+
global sum, loopsum
9+
10+
for i in range(1,loopsum):
11+
lock.acquire()
12+
sum += 1
13+
lock.release()
14+
15+
def myMinu():
16+
global sum, loopsum
17+
for i in range(1, loopsum):
18+
lock.acquire()
19+
sum -= 1
20+
lock.release()
21+
22+
if __name__ == '__main__':
23+
print("Starting ......{}".format(sum ))
24+
t1 = threading.Thread(target=myadd(), args=())
25+
t2 = threading.Thread(target=myMinu(), args=())
26+
t1.start()
27+
t2.start()
28+
29+
t1.join()
30+
t2.join()
31+
32+
print("Done .....{}".format(sum))

LOG10/05.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import threading
2+
import time
3+
import queue
4+
5+
class Producer(threading.Thread):
6+
def run(self):
7+
global queue
8+
count = 0
9+
while True:
10+
if queue.qsize()<1000:
11+
for i in range(100):
12+
count = count + 1
13+
msg = '生成产品' + str(count)
14+
queue.put(msg)#放入消息
15+
print(msg)
16+
time.sleep(0.5)
17+
18+
class Consumer(threading.Thread):
19+
def run(self):
20+
global queue
21+
while True:
22+
if queue.qsize()> 100:
23+
for i in range(3):
24+
msg = self.name + '消费了' + queue.get()#拿出消息
25+
print(msg)
26+
time.sleep(1)
27+
28+
if __name__ == '__main__':
29+
queue=queue.Queue()
30+
for i in range(500):
31+
queue.put("初始产品" + str(i))
32+
for i in range(2):
33+
p = Producer()
34+
p.start()
35+
for i in range(5):
36+
c = Consumer()
37+
c.start()

LOG10/06.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import threading
2+
import time
3+
4+
lock_1 = threading.Lock()
5+
lock_2 = threading.Lock()
6+
7+
def fun1():
8+
print("func1 starting......")
9+
lock_1.acquire()
10+
print("func1 acquir1 lock_1")
11+
time.sleep(2)
12+
print("func_1 等待lock_2")
13+
lock_2.acquire()
14+
print("func_1 acquire lock_2......")
15+
16+
lock_2.release()
17+
print("func_1 relesse lock_2")
18+
19+
lock_1.release()
20+
print("func1 release lock_1")
21+
22+
print("func1 done......")
23+
24+
25+
def func_2():
26+
print("func2 starting......")
27+
lock_2.acquire()
28+
print("func2 acquir1 lock_2")
29+
time.sleep(4)
30+
print("func_2 等待lock_1")
31+
lock_1.acquire()
32+
print("func_2 acquire lock_1......")
33+
34+
lock_1.release()
35+
print("func_2 relesse lock_1")
36+
37+
lock_2.release()
38+
print("func1 release lock_2")
39+
40+
print("func1 done......")
41+
42+
43+
if __name__ == '__main__':
44+
print("主程序启动")
45+
t1 = threading.Thread(target=fun1, args=())
46+
t2 = threading.Thread(target=func_2, args=())
47+
48+
t1.start()
49+
t2.start()
50+
51+
t1.join()
52+
t2.join()

LOG10/07.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import threading
2+
import time
3+
4+
lock_1 = threading.Lock()
5+
lock_2 = threading.Lock()
6+
7+
def fun1():
8+
print("func1 starting......")
9+
lock_1.acquire(timeout = 4)
10+
print("func1 acquir1 lock_1")
11+
time.sleep(2)
12+
print("func_1 等待lock_2")
13+
rst=lock_2.acquire()
14+
if rst:
15+
print("func_1 acquire lock_2......")
16+
lock_2.release()
17+
print("func_1 relesse lock_2")
18+
else:
19+
print("fun1 not acquire lock_2")
20+
21+
lock_1.release()
22+
print("func1 release lock_1")
23+
24+
print("func1 done......")
25+
26+
27+
def func_2():
28+
print("func2 starting......")
29+
lock_2.acquire()
30+
print("func2 acquir1 lock_2")
31+
time.sleep(4)
32+
print("func_2 等待lock_1")
33+
34+
rst = lock_1.acquire(timeout=4)
35+
if rst:
36+
print("func_2 acquire lock_1......")
37+
38+
lock_1.release()
39+
print("func_2 relesse lock_1")
40+
else:
41+
print("func_2 not acquire lock_1")
42+
43+
44+
lock_2.release()
45+
print("func1 release lock_2")
46+
47+
print("func1 done......")
48+
49+
50+
if __name__ == '__main__':
51+
print("主程序启动")
52+
t1 = threading.Thread(target=fun1, args=())
53+
t2 = threading.Thread(target=func_2, args=())
54+
55+
t1.start()
56+
t2.start()
57+
58+
t1.join()
59+
t2.join()
60+

LOG10/08.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import threading
2+
import time
3+
# 参数定义了最多有几个线程同时使用资源
4+
semaphore = threading.Semaphore(3)
5+
6+
def func():
7+
if semaphore.acquire():
8+
for i in range(5):
9+
print(threading.currentThread().getName() + 'get semaphore')
10+
time.sleep(15)
11+
semaphore.release()
12+
print(threading.currentThread().getName() + 'release semaphore')
13+
14+
for i in range(8):
15+
t1 = threading.Thread(target=func,args=())
16+
t1.start()

LOG10/09.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import threading
2+
import time
3+
4+
def func():
5+
print("I am running......")
6+
time.sleep(4)
7+
print("I am done......")
8+
9+
if __name__ == '__main__':
10+
t = threading.Timer(6, func)
11+
t.start()
12+
13+
i = 0
14+
while True:
15+
print("{}********".format(i))
16+
time.sleep(1)
17+
i += 1

LOG10/10.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import threading
2+
import time
3+
4+
class MyThread(threading.Thread):
5+
def run(self):
6+
global num
7+
time.sleep(1)
8+
9+
if mutex.acquire(1):
10+
num += 1
11+
msg = self.name +'set num to ' + str(num)
12+
print(msg)
13+
mutex.acquire()
14+
mutex.release()
15+
mutex.release()
16+
17+
num = 0
18+
mutex = threading.RLock()
19+
def tes1t():
20+
for i in range(5):
21+
t = MyThread()
22+
t.start()
23+
24+
if __name__ == '__main__':
25+
tes1t()

0 commit comments

Comments
 (0)