簡體   English   中英

在Python中同時運行函數

[英]Running Functions Simultaneously in Python

我的IRC頻道有一個漫游器,我正在嘗試讓它提醒人們現在仍然是互動的時間。

代碼如下:

*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()

我認為問題在於兩個流程都有

while 1:

循環進入它們,並需要知道如何在1:循環同時運行。 我查看了有關多處理與線程的文檔和示例,以及SO上的其他示例,令我感到困惑的是,我看到的所有示例都將參數傳遞給它們,而我只是在嘗試運行這些流程不需要任何參數。

最簡單的形式是:有沒有一種方法可以一次運行兩個彼此獨立的函數,每個函數都包含while循環?

在@eyllansec的評論中提及

我需要改變

t1 = Thread(target = process1()) 

t1 = Thread(target = process1)

現在完全按預期工作,謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM