One 、 Timer Use a timer to trigger some events
Example :
import threading
import time
def run():
print(' Timer on ')
timer = threading.Timer(5,run) # Re enable the timer , Realize the cycle
timer.start()
if __name__ == '__main__':
t1 = threading.Timer(1,function=run) # Create a timer
#5 Start thread in seconds # Method
t1.start()Two 、 queue
''' queue.Queue() First in, first out queue.LifoQueue() After the first out queue.PriorityQueue() Priority can be set Transmissible parameters maxsize Set the amount of data that can be stored queue.LifoQueue() After the first out queue.PriorityQueue() Priority can be set Transmissible parameters maxsize Set the amount of data that can be stored Queue.qsize() The amount of data obtained Queue.full() Determine if the queue is full Queue.empty() Determine whether the queue is empty Queue.join() Wait for the thread to complete ''' import queue q = queue.Queue(maxsize=3) # Create a queue The maximum capacity is 3 q.put(1)# Put in numbers 1 q.put(2) q.put(3) # q.put(4) # Put in an individual that exceeds the maximum capacity , The program will wait for the individual inside to take out # q.put(4,False,timeout=3)# After setting the waiting time , Will wait for 3 Seconds later , Application error print(q.queue) # Print out the individuals in the queue : deque([1, 2, 3]) print(q.qsize()) # Print the number of individuals in the queue : 3 q.get() # Take out one individual in the queue in turn print(q.full()) # Judge whether the queue is full Print as False or Ture print(q.empty()) # Judge whether the queue is full Print as False or Ture print(q.get()) # Print the individuals taken out this time # q1 = queue.PriorityQueue() Priority can be set # q1.put(2,'nihao1') # q1.put(5,'nihao2') # ( priority , Put elements ) producer 、 Classic consumer cases
''' Producer consumer model '''
import queue
import threading
import time
q = queue.Queue(maxsize=10) # Create a queue
def put_in():
count = 1
while True:
q.put(f' Brown sugar Ciba {count}') # Put in
print(f' Brown sugar Ciba {count} Out of the pot ')
count += 1
time.sleep(1) # Sleep for a second
def get_out(name):
while True: # Cycle all the time
print(f'{name} Ate {q.get()}')
time.sleep(1)
if __name__ == '__main__':
p = threading.Thread(target=put_in) # Create thread , Call function
p.start()
g = threading.Thread(target=get_out,args=('qq',)) # Create thread , Call function
# The incoming object should be in the form of tuple (a,b)
g.start()
Thread pool :http://c.biancheng.net/view/2627.html
Python3 MySQL Database connection – PyMySQL drive | Novice tutorial (runoob.com)
I, a sophomore, earned 8929.6 yuan by taking a part-time job in Python during the holiday
For more than half a year , Th
[Python from entry to mastery] (28) 56,000 words to make a conclusion on the basic knowledge of Python [worth collecting]
為什麼寫這篇文章我從2021年6月13號寫下第一篇Pytho