Python:等待时接受输入

时间:2015-08-29 12:28:49

标签: python input timer wait

我有一个小脚本,我写了每5分钟执行一次任务(这不一定是准确的),但是因为它是当前写的唯一可以取消的方法是使用Ctrl + Z(linux终端)并且无法在不重新启动脚本的情况下更改它执行的任务集。

因此,我希望更改脚本以便能够在等待进行下一次运行时接受用户输入而不会中断任何大量的5分钟计时器(正如杀死脚本并重新启动当前所做的那样),但我无法弄清楚如何让protected void Button1_Click(object sender, EventArgs e) { // define your connection string (typically from a .config file) and your query WITH parameters! string connectionString = "Data Source=(local);Initial Catalog=payroll;Integrated Security=True"; string query = "SELECT employeeid, role FROM employees WHERE username=@user AND and password=@pwd;"; // set up a connection and command in using() blocks using (SqlConnection con = new SqlConnection(connectionString)) using (SqlCommand cmd = new SqlCommand(query, con)) { // add parameters and set their values cmd.Parameters.Add("@user", SqlDbType.VarChar, 50).Value = TextBox1.Text; cmd.Parameters.Add("@pwd", SqlDbType.VarChar, 50).Value = TextBox2.Text; // open connection con.Open(); // establish data reader using (SqlDataReader dr = cmd.ExecuteReader()) { // if at least one row is returned.... if (dr.Read()) { // get employee ID and role from the reader string employeeId = dr.GetFieldValue<string>(0); string role = dr.GetFieldValue<string>(1); // depending on role, jump off to various pages switch (role) { case "admin": Response.Redirect("Admin.aspx"); break; case "manager": Response.Redirect("manager.aspx"); break; default: Response.Redirect("employee.aspx"); break; } } else { // what do you want to do if NO ROW was returned? E.g. user/pwd combo is wrong } dr.Close(); } con.Close(); } } 仅接受给定时间的值等。此外,当前代码是用Python 2.7编写的,但我和#39;很高兴转换为3,如果这使解决方案更容易。

当前代码如下所示:

raw input

有什么想法吗?

更新:此问题没有导致发布特定的代码解决方案,因此这里是a later question的链接,其中包含我实施的解决方案的简单版本(以及所需的一个简单编辑)它在答案中工作)/

1 个答案:

答案 0 :(得分:1)

我想你想要那样的东西。

import threading

def takeInput():
    """This function will be executed via thread"""
    value = raw_input("what you want:")
    return

#initialize how you want
value = 0 
t = threading.Thread(target=takeInput)
t.start()
time.sleep(5)