1
正如標題所述,如何在執行其他命令時執行其他命令? 可以說,假設我有這樣的:正在執行其他命令
import urllib.request
import re
class runCommands:
def say(self,word):
return word
def rsay(self,word):
return word[::-1]
def urban(self,term):
data = urllib.request.urlopen("http://urbandictionary.com/define.php?term=%s" % term).read().decode()
definition = re.search('<div class="definition">(.*?)</div>',data).group(1)
return definition
def run(self):
while True:
command = input("Command: ")
command,data = command.split(" ",1)
if command == "say": print(self.say(data))
if command == "reversesay": print(self.rsay(data))
if command == "urbandictionary": print(self.urban(data))
現在,我認識到,做runCommands()的run()我必須輸入命令一次一個,但假設,如果我能像這樣一些如何輸入多個命令:
me: "urbandictionary hello"
me: "reverse hello" # before it posts the result
我將如何得到它同時運行,即使它實際上做「城市詞典你好」,然後「反向你好」第二聽說纖維性能做到這一點,但我不知道我怎麼會那樣做與線程。是否有線程唯一的選擇,讓它實際上發佈「olleh」,然後它將返回城市字典結果作爲hello,即使我先執行了「urbandictionary hello」?
你需要[主題](http://docs.python.org/2/library/thread.html)■! – 2013-10-29 23:49:19
'線程'。你也來自java背景?你不需要一個班級去做所有事情,當然不是上述事情。 – roippi
你不需要線程。你可以使用Twisted,grequests,甚至子進程。它看起來像你在Python 3,所以你也可以給asyncio一個旋風。 – Eevee