Khởi động lại bộ hẹn giờ luồng Python

ph. D. / Golden Gate Ave, San Francisco / Đại học Quốc gia Seoul / Carnegie Mellon / UC Berkeley / DevOps / Học sâu / Trực quan hóa


Tài trợ các hoạt động phát triển Nguồn mở và nội dung miễn phí cho mọi người

Cảm ơn bạn

- Kh Hồng

Steven D'Aprano steve tại lê. thông tin
Thứ tư ngày 14 tháng 11 00. 50. 51 CET 2012
On 14/11/12 02:42, Patrick Dickey wrote:
> Hi everyone,
>
> I've got an application that will use a timer to run a function
> automatically [it's an update function for my IPv6 endpoint]. The
> questions that I have are these:

Have you read the documentation?

//docs.python.org/2/library/threading.html#timer-objects

> 1.  Can I stop and start the timer from different functions or methods
> in my program, and if so, how?

As the docs say:

[quote]
Timers are started, as with threads, by calling their start[] method.
The timer can be stopped [before its action has begun] by calling
the cancel[] method.
[end quote]

Any function or method that has access to the timer object can call
the start or cancel method. You just need to make sure that the
function can see the timer object, using one of the standard ways
in Python of giving a function access to any other object:

* [best] pass the object into the function as an argument

   result = myfunction[a, b, c, some_timer]

* [easiest] make the timer a global variable

* put the timer inside a dict, list or other container and
   pass it to the function

* put the timer in an attribute of the object

etc.



> 2.  Can I stop the timer, change the value, and restart it [or would it
> create a new timer], or do I have to create a new timer with an entirely
> new name?

Changing the timer is not supported. I expect that calling my_timer.start[]
after cancelling it would restart it, but haven't tested it.

Just because you create a new timer doesn't mean you have to give it an
entirely new name.


> 3.  If I get a value from a textbox, how do I parse it from the string
> value to an integer [or float]?

This has nothing to do with timers, and should go into a separate email
thread so that those people who know nothing about threading can contribute.

Regardless of where the string comes from, you turn a string into an int or
float by calling the int[] or float[] function.


> 4.  Is there a better way of accomplishing this task?
>
> Here's the pseudocode for what I'm doing.
>
> if autoUpdates is enabled
>     get updateFrequency
>     start timer with time value from updateFrequency
>     when time is reached, run update method
> else
>     cancel timer


Well that won't work, because once the update has run, it will stop
checking for new updates.


> if autoUpdates is enabled AND user changes updateFrequency
>     stop timer
>     get updateFrequency
>     start timer with new time value from updateFrequency

Seems fine to me.



-- 
Steven
Thông tin thêm về danh sách gửi thư Tutor
def start_internal[self, color, log=True]. """Bắt đầu đồng hồ bên trong. """ nếu không tự. internal_running[]. nếu tự. chế độ trong [TimeMode. BLITZ, Chế độ thời gian. FISCHER]. bản thân. active_color = màu tự. reset_start_time[] nếu đăng nhập. w_hms, b_hms = tự. _log_time[] ghi nhật ký. thông tin ['bắt đầu thời gian nội bộ w. %s - b. %s [ign]', w_hms, b_hms] ghi nhật ký. thông tin ['thời gian đồng hồ nhận được w. %s - b. %s [sử dụng]', hms_time[self. clock_time[cờ vua. TRẮNG]], hms_time[tự. clock_time[cờ vua. ĐEN]]] mình. internal_time[cờ vua. TRẮNG] = bản thân. clock_time[cờ vua. TRẮNG] tự. internal_time[cờ vua. ĐEN] = bản thân. clock_time[cờ vua. ĐEN] # Chỉ bắt đầu chuỗi nếu chưa bắt đầu cho cùng màu và người chơi chưa bị mất thời gian nếu tự. internal_time[color] > 0 và tự. active_color không phải là Không có và tự. run_color. = bản thân. active_color. bản thân. hẹn giờ = luồng. Hẹn giờ [sao chép. bản sao [tự. internal_time[color]], tự. _out_of_time, [bản sao. bản sao [tự. internal_time[color]]]] self. hẹn giờ. bắt đầu [] ghi nhật ký. gỡ lỗi ['bắt đầu hẹn giờ nội bộ - màu. %s chạy. %s đang hoạt động. %s', màu sắc, bản thân. run_color, bản thân. active_color] tự. run_color = tự. active_color

Trình bao bọc cho threading.Timer để cung cấp triển khai Bộ hẹn giờ có thể đặt lại. Cũng cung cấp bộ đếm thời gian giả để thử nghiệm

Cách sử dụng

Hẹn giờ có thể đặt lại

from resettabletimer import ResettableTimer

delay = 5 # seconds
function = print

# Create resettable timer
t = ResettableTimer[delay, function, ["Hello"], {"end":" timer!\n"}]

# Starting and canceling work similarly than with threading.Timer
t.start[]

# Wait 1-5 seconds

# Reset the timer
t.reset[]

# Hello should be printed after five seconds

FakeTimer

from resettabletimer import FakeTimer

t = FakeTimer[2, print, ["Hello"], {"end":" timer!\n"}]

# Starting and canceling work similarly than with threading.Timer
t.start[]

# Wait >2 seconds
# Nothing happens

# Time passage is controlled with pass_time
t.pass_time[2]

# Hello should be printed

thử nghiệm

Kiểm tra và tự động sửa định dạng với

pycodestyle resettabletimer
autopep8 -aaar --in-place resettabletimer

Chạy phân tích tĩnh với

pylint -E --enable=invalid-name,unused-import,useless-object-inheritance resettabletimer

Chạy thử nghiệm đơn vị

# Run unit tests
python3 -m unittest discover -s tst/

# Run unit tests with coverage analysis
coverage run \
    --branch \
    --source resettabletimer/ \
    -m unittest discover -s tst/
coverage report -m

Python có đợi chủ đề kết thúc không?

Giống như hầu hết các ngôn ngữ lập trình, trong Python cũng có các luồng. Mã thực thi tuần tự, nghĩa là mọi chức năng đợi chức năng trước đó hoàn thành trước khi có thể thực thi .

Các chủ đề có thể sinh ra các quy trình không?

Một luồng là một chuỗi các hướng dẫn đang được thực thi trong ngữ cảnh của một quy trình. Một tiến trình có thể sinh ra nhiều luồng nhưng tất cả chúng sẽ chia sẻ cùng một bộ nhớ.

Chủ Đề