import gevent
from gevent import monkey
import time
# 打補丁,讓程式能夠識別系統的耗時操作以及網路資源請求的耗時操作
monkey.patch_all()
def work1():
# 獲取當前協程
print(gevent.getcurrent())
while true:
print('---work1----')
time.sleep(0.5)
def work2():
while true:
print('----work2----')
time.sleep(0.5)
def main():
# 建立協程
g1 = gevent.spawn(work1)
g2 = gevent.spawn(work2)
# 主線程等待協程結束後才結束
g1.join()
g2.join()
if __name__ == '__main__':
main()
12python切片實現trim()
利用切片操作,實現乙個trim 函式,去除字串首尾的空格,注意不要呼叫str的strip 方法 用於移除字串頭尾指定的字元 預設為空格或換行符 或字串行。test 000python000world000 print test.strip 0 執行結果 python000worlddef trim ...
12 Python 檔案處理
資料夾 得到當前工作目錄,即當前python指令碼工作的目錄路徑 os.getcwd 返回指定目錄下的所有檔案和目錄名 os.listdir 函式用來刪除乙個檔案 os.remove 刪除多個目錄 os.removedirs r c python 檢驗給出的路徑是否是乙個檔案 os.path.isf...
12 python基礎 函式
12.1 函式簡介一段具有特定功能的 可重用的語句組 函式規則 1.def 2.return 表示式 結束函式,不帶表示式的return相當於返回 none 作用 降低程式設計難度和 復用def 函式名 引數 引數是佔位符 函式體return 返回值 引數是輸入 函式體是處理 結果是輸出 ipo 函...