简体   繁体   English

在Python中同时运行函数

[英]Running Functions Simultaneously in Python

I have a bot for our IRC channel, and I'm trying to have it remind people what time it is while still being interactive. 我的IRC频道有一个漫游器,我正在尝试让它提醒人们现在仍然是互动的时间。

The code looks like: 代码如下:

*code that logs into the server*
def process1():
    while 1:
        time = datetime.datetime.now().time()
        time = str(time).split(':')
        if '15' in time[1]:
            irc.send('PRIVMSG ' + channel + ' :It is %s:%s, rest of message' % (time[0],time[1]))

def process2():
    while 1:
        *code that listens and responds to what comes in from other users*

if __name__ == '__main__':
    t1 = Thread(target = process1())
    t2 = Thread(target = process2())
    t1.setDaemon(True)
    t2.setDaemon(True)
    t1.start()
    t2.start()

I'm thinking the issue is both processes have a 我认为问题在于两个流程都有

while 1:

Loop in them, and need to know how to run both while 1: loops simultaneously. 循环进入它们,并需要知道如何在1:循环同时运行。 I've looked at the documentation and examples for multiprocessing vs threading, along with other examples on SO and what confuses me about multiprocessing is all the examples I've seen have arguments passed onto them, where I'm just trying to run these processes that don't require any arguments. 我查看了有关多处理与线程的文档和示例,以及SO上的其他示例,令我感到困惑的是,我看到的所有示例都将参数传递给它们,而我只是在尝试运行这些流程不需要任何参数。

In its simplest form: is there a way to run two functions at once, both containing while loops, independent of each other? 最简单的形式是:有没有一种方法可以一次运行两个彼此独立的函数,每个函数都包含while循环?

Mentioned in the comments by @eyllansec 在@eyllansec的评论中提及

I needed to change 我需要改变

t1 = Thread(target = process1()) 

to

t1 = Thread(target = process1)

Works exactly as intended now, thank you! 现在完全按预期工作,谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM