• <table id="ceegc"></table>
  • <td id="ceegc"><option id="ceegc"></option></td>
  • <optgroup id="ceegc"></optgroup>
  • <td id="ceegc"></td>
  • <table id="ceegc"></table>
  • 發布時間:2020-10-05 19:06 原文鏈接: python修煉day29(二)

    在這里碰見一個問題:

    Linux 中客戶端的代碼可以和別人 Windows 中服務器的代碼可以鏈接,但是無法下載, 不知道是因為系統是原因還是什么其他原因。

    線程練習代碼

    簡單的多任務線程

    import threading

    import time

    def sing():

    """唱歌"""

    for i in range(5):

    print("---正在唱歌----")
           time.sleep(1)

    def dance():

    """跳舞"""

    for i in range(5):
           print("---正在跳舞---")
           time.sleep(1)

    def main():

    # 創建對象

    t1 = threading.Thread(target=sing)

    t2 = threading.Thread(target=dance)

    # 啟動子線程

    t1.start()

    t2.start()

    if __name__ == '__main__':

    main()

    多任務——繼承

    import threading

    """由繼承來實現多任務"""

    class MyThread(threading.Thread):

    def __init__(self, num):  # 重寫父類方法

    super().__init__()

    self.num = num

    def run(self):   # 重寫父類方法

    for i in range(self.num):

    print(i)

    if __name__ == '__main__':

    t1 = MyThread(10)

    t1.start()  # 調用父類中的start方法會創建一個子線程, 子線程會自動調用子類中的run方法

    互斥鎖解決資源競爭問題

    import threading

    import time

    # 新建全局變量

    gl_nums = 0

    # 新建鎖

    # 使用鎖來解決資源競爭問題

    mutex = threading.Lock()

    def test1(num):

    global gl_nums

    for i in range(num):
           # 上鎖
           mutex.acquire()
           gl_nums += 1
           # 解鎖
           mutex.release()

    print("----test1  %d----" % gl_nums)

    def test2(num):

    global gl_nums

    for i in range(num):
           # 上鎖
           mutex.acquire()
           gl_nums += 1
           # 解鎖
           mutex.release()

    print("----test2  %d----" % gl_nums)

    def main():

    t1 = threading.Thread(target=test1, args=(1000000,))

    t2 = threading.Thread(target=test2, args=(1000000,))

    t1.start()

    t2.start()

    time.sleep(2)

    print("---main  %d----" % gl_nums)

    if __name__ == '__main__':

    main()

    線程聽了個迷迷糊糊,把代碼多敲幾遍吧。。。

    剛才吃完飯回來抽空研究了一下如何翻墻,嗯,折騰了一會,成功翻了出去,一直聽說墻外的世界很精彩,終于可以一睹真面目了。。。

    對于如何翻墻感興趣的可以聯系我。


  • <table id="ceegc"></table>
  • <td id="ceegc"><option id="ceegc"></option></td>
  • <optgroup id="ceegc"></optgroup>
  • <td id="ceegc"></td>
  • <table id="ceegc"></table>
  • www.mitao95.com