Python:传递一个接受类方法参数的函数?

时间:2017-09-20 12:12:44

标签: python multithreading class timer

亲爱的所有请考虑这是我在Python的第一周,我正在尝试创建一个线程函数,创建调用特定函数的计时器。这是我的代码:

import threading
class TimerClass(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.event = threading.Event()
        self.event.set = False
    def run(self, timer_wait, my_fun(print_text)):
        while True:
            my_fun(print_text)
            self.event.wait(timer_wait)

    def stop(self):
        self.event.set = True




def my_fun(text_to_print):
    print(text_to_print)


tmr = TimerClass()
tmr.run(3, my_fun('hello world'))

此代码的结果是

def run(self, timer_wait, my_fun(print_text))
                                  ^
SyntaxError: invalid syntax

如何更正此代码? 非常感谢!

1 个答案:

答案 0 :(得分:1)

单独传递参数:

@Bean
public DataSource restDataSource(){
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
    dataSource.setUrl(env.getProperty("jdbc.url"));
    dataSource.setUsername(env.getProperty("jdbc.user"));
    dataSource.setPassword(env.getProperty("jdbc.pass"));
    return dataSource;
}

并称之为:

@Value("${jdbc.driverClassName}")
private String driverClassName;

@Value("${jdbc.url}")
private String url;

@Value("${jdbc.user}")
private String username;

@Value("${jdbc.pass}")
private String password;

@Bean
public DataSource restDataSource(){
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    return dataSource;
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholder() {
    return new PropertySourcesPlaceholderConfigurer();
}