python 中的线程休眠中断(来自 java 的端口)
Thread sleep with interrupt in python (port from java)
我有以下 Java class 想要(在概念上)移植到 python。
这个想法是你有一个闹钟线程,它会休眠 x 秒(直到第二天早上),除非设置了新的唤醒时间,此时休眠线程被中断,新的剩余时间被设置并且它那个时候睡觉。如果完成睡眠,则触发闹钟声音并等待设置新的唤醒时间
我想将它移植到 Python,但我只是花了几个小时在谷歌上搜索,虽然有 1001 种方法可以管理线程并在 Python 中休眠,但我找不到如何休眠 ()持续 x 秒,但也发送中断。
需要说明的是,我不需要有人为我编写整个 class,只需一个简单的睡眠和中断示例就足够了,这样我就可以理解它在 [=23] 中的完成方式=].
package com.njitram.bedroomtunes.server.alarm;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.njitram.bedroomtunes.log.Logger;
public class AlarmThread extends Thread {
private Calendar wakeupTime;
private Alarm alarm;
/*
* Constructor to disable the alarm
*/
public AlarmThread(Alarm alarm) {
this(null, alarm);
}
public AlarmThread(Calendar wakeupTime, Alarm alarm) {
this.alarm = alarm;
if(wakeupTime == null) {
disable();
} else {
setNewWakeUpTime(wakeupTime);
}
this.start();
}
public void setNewWakeUpTime(Calendar wakeupTime) {
Logger.log("New Wake time set for AlarmThread: " + new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(wakeupTime.getTime()));
this.wakeupTime = wakeupTime;
// If the thread was already started, it will be sleeping. Wake it up and recalculate how long it needs to sleep. Interrupting will achieve this.
this.interrupt();
}
public void disable() {
setNewWakeUpTime(getDisabledTime());
}
private Calendar getDisabledTime() {
// The idea is to disable the alarm. If the alarm eventually goes off in the year 3000, I deserve to wake up...
Calendar wakeupTime = Calendar.getInstance();
wakeupTime.set(Calendar.YEAR, 3000);
return wakeupTime;
}
@Override
public void run() {
while(true) {
try {
long sleepTime = getSleepTime();
Logger.log("Sleeping for "+sleepTime);
// Sleep until it is time for the alarm to go off. This can be interrupted if a new wakeUpTime is set
Thread.sleep(sleepTime);
// After sleeping, wake up
wakeUp();
// Wait for the new time to be set and the alarm to send an interrupt to continue the thread
synchronized(this) {
wait();
}
} catch (InterruptedException e) {
Logger.log("Interrupted");
/* The thread can be interrupted when a new wake-up time has been set */
}
}
}
private long getSleepTime() {
return wakeupTime.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
}
private void wakeUp() {
alarm.wakeUp();
}
}
APScheduler package seems to provide the functionality you are looking for. The user guide 应该包含有关设置和删除日程安排所需的所有功能的信息。
请注意,这与您的睡眠方法不同,它使用调度代替 - 尽管我建议不要 'busy sleeping',因为它会浪费 CPU 个周期
我有以下 Java class 想要(在概念上)移植到 python。
这个想法是你有一个闹钟线程,它会休眠 x 秒(直到第二天早上),除非设置了新的唤醒时间,此时休眠线程被中断,新的剩余时间被设置并且它那个时候睡觉。如果完成睡眠,则触发闹钟声音并等待设置新的唤醒时间
我想将它移植到 Python,但我只是花了几个小时在谷歌上搜索,虽然有 1001 种方法可以管理线程并在 Python 中休眠,但我找不到如何休眠 ()持续 x 秒,但也发送中断。
需要说明的是,我不需要有人为我编写整个 class,只需一个简单的睡眠和中断示例就足够了,这样我就可以理解它在 [=23] 中的完成方式=].
package com.njitram.bedroomtunes.server.alarm;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.njitram.bedroomtunes.log.Logger;
public class AlarmThread extends Thread {
private Calendar wakeupTime;
private Alarm alarm;
/*
* Constructor to disable the alarm
*/
public AlarmThread(Alarm alarm) {
this(null, alarm);
}
public AlarmThread(Calendar wakeupTime, Alarm alarm) {
this.alarm = alarm;
if(wakeupTime == null) {
disable();
} else {
setNewWakeUpTime(wakeupTime);
}
this.start();
}
public void setNewWakeUpTime(Calendar wakeupTime) {
Logger.log("New Wake time set for AlarmThread: " + new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(wakeupTime.getTime()));
this.wakeupTime = wakeupTime;
// If the thread was already started, it will be sleeping. Wake it up and recalculate how long it needs to sleep. Interrupting will achieve this.
this.interrupt();
}
public void disable() {
setNewWakeUpTime(getDisabledTime());
}
private Calendar getDisabledTime() {
// The idea is to disable the alarm. If the alarm eventually goes off in the year 3000, I deserve to wake up...
Calendar wakeupTime = Calendar.getInstance();
wakeupTime.set(Calendar.YEAR, 3000);
return wakeupTime;
}
@Override
public void run() {
while(true) {
try {
long sleepTime = getSleepTime();
Logger.log("Sleeping for "+sleepTime);
// Sleep until it is time for the alarm to go off. This can be interrupted if a new wakeUpTime is set
Thread.sleep(sleepTime);
// After sleeping, wake up
wakeUp();
// Wait for the new time to be set and the alarm to send an interrupt to continue the thread
synchronized(this) {
wait();
}
} catch (InterruptedException e) {
Logger.log("Interrupted");
/* The thread can be interrupted when a new wake-up time has been set */
}
}
}
private long getSleepTime() {
return wakeupTime.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
}
private void wakeUp() {
alarm.wakeUp();
}
}
APScheduler package seems to provide the functionality you are looking for. The user guide 应该包含有关设置和删除日程安排所需的所有功能的信息。
请注意,这与您的睡眠方法不同,它使用调度代替 - 尽管我建议不要 'busy sleeping',因为它会浪费 CPU 个周期