Hướng dẫn settimeout python - Python settimeout

Trong thực tế, một bộ đếm thời gian có lẽ là cách đơn giản nhất để làm những gì bạn muốn.

Mã này sẽ làm như sau:

  • Sau 1 giây, nó in "arg1 arg2"
  • Sau 2 giây, nó in "Owls Owls Owls"

===

from threading import Timer

def twoArgs(arg1,arg2):
    print arg1
    print arg2
    print ""

def nArgs(*args):
    for each in args:
        print each

#arguments: 
#how long to wait (in seconds), 
#what function to call, 
#what gets passed in
r = Timer(1.0, twoArgs, ("arg1","arg2"))
s = Timer(2.0, nArgs, ("OWLS","OWLS","OWLS"))

r.start()
s.start()

===

Mã trên rất có thể sẽ giải quyết vấn đề của bạn.

Nhưng mà! Có nhiều cách khác, không sử dụng đa luồng. Nó hoạt động giống như JavaScript, được một luồng đơn.

Đối với phiên bản duy nhất này, tất cả những gì bạn cần làm là lưu trữ chức năng và các đối số của nó trong một đối tượng, cùng với thời gian mà chức năng nên được chạy.

Khi bạn có đối tượng chứa lệnh gọi chức năng và thời gian chờ, chỉ cần kiểm tra định kỳ xem hàm có sẵn sàng thực thi không.

Cách đúng đắn để làm điều này là bằng cách thực hiện hàng đợi ưu tiên để lưu trữ tất cả các chức năng chúng tôi muốn chạy trong tương lai, như trong mã bên dưới.

Giống như trong JavaScript, cách tiếp cận này không đảm bảo rằng chức năng sẽ được chạy chính xác đúng hạn. Một chức năng mất một thời gian rất dài để chạy sẽ trì hoãn các chức năng sau khi nó. Nhưng nó đảm bảo rằng một chức năng sẽ được chạy không sớm hơn thời gian chờ của nó.

Mã này sẽ làm như sau:

  • Sau 1 giây, nó in "20"
  • Sau 2 giây, nó in "132"
  • Sau 3 giây, nó bỏ đi.

===

from datetime import datetime, timedelta
import heapq

# just holds a function, its arguments, and when we want it to execute.
class TimeoutFunction:
    def __init__(self, function, timeout, *args):
        self.function = function
        self.args = args
        self.startTime = datetime.now() + timedelta(0,0,0,timeout) 

    def execute(self):
        self.function(*self.args)

# A "todo" list for all the TimeoutFunctions we want to execute in the future
# They are sorted in the order they should be executed, thanks to heapq
class TodoList: 
    def __init__(self):
        self.todo = []

    def addToList(self, tFunction):
        heapq.heappush(self.todo, (tFunction.startTime, tFunction))

    def executeReadyFunctions(self):
        if len(self.todo) > 0:
            tFunction = heapq.heappop(self.todo)[1]
            while tFunction and datetime.now() > tFunction.startTime:
                #execute all the functions that are ready
                tFunction.execute()
                if len(self.todo) > 0:
                    tFunction = heapq.heappop(self.todo)[1]
                else:
                    tFunction = None                    
            if tFunction:
                #this one's not ready yet, push it back on
                heapq.heappush(self.todo, (tFunction.startTime, tFunction))

def singleArgFunction(x):
    print str(x)

def multiArgFunction(x, y):
    #Demonstration of passing multiple-argument functions
    print str(x*y)

# Make some TimeoutFunction objects
# timeout is in milliseconds
a = TimeoutFunction(singleArgFunction, 1000, 20)
b = TimeoutFunction(multiArgFunction, 2000, *(11,12))
c = TimeoutFunction(quit, 3000, None)

todoList = TodoList()
todoList.addToList(a)
todoList.addToList(b)
todoList.addToList(c)

while True:
    todoList.executeReadyFunctions()

===

Mã trên rất có thể sẽ giải quyết vấn đề của bạn.

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn dấu tab trong python

Không gian không được coi là tương đương với tab. Một dòng thụt vào với một tab nằm ở một vết lõm khác nhau từ một dòng thụt vào với 1, 2, 4 hoặc 8 ...

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn dùng plots matplotlib python

Trong hướng dẫn trước của chúng tôi, Làm sạch dữ liệu Python . Hôm nay, chúng ta sẽ chơi với Hướng dẫn Python Matplotlib và Cốt truyện Python. Hơn nữa, ...

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn dùng regex one python

Nội dung chính Regex trong Python Các hàm Regex Xây dựng biểu thức chính quyMeta-CharactersKý tự đặc biệtSet Hàm findall() Đối tượng Match (kết quả khớp) Các ...

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn how do i store a character in a list python? - làm cách nào để lưu trữ một ký tự trong một python danh sách?

Sử dụng danh sách hiểu biết:In [24]: s = [ABC,DEF,GHI,JKL] In [25]: lis=[list(x) for x in s] In [26]: lis Out[26]: [[A, B, C], [D, E, F], [G, H, I], [J, K, ...

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn dùng sessiom trong PHP

Khái niệm CookieLưu CookieĐọc CookiePHP CookieKhái niệm về Session PHPSử dụng SessionHủy SessionCookie là mẩu tin nhỏ được lưu ở máy người dùng (cụ thể là ...

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout

Hướng dẫn settimeout python - Python settimeout