Python多執行緒實現同時執行兩個while迴圈

2021-08-19 11:14:20 字數 977 閱讀 4409

如果想同時執行兩個while true迴圈,可以使用多執行緒threading來實現。

完整**

#coding=gbk

from time import sleep, ctime 

import threading

def muisc(func):

while true:

print 'start playing: %s! %s' %(func,ctime())

sleep(2)

def move(func):

while true:

print 'start playing: %s! %s' %(func,ctime())

sleep(5)

def player(name):

r = name.split('.')[1]

if r == '***':

muisc(name)

else:

if r == 'mp4':

move(name)

else:

print 'error: the format is not recognized!'

list = ['愛情買賣.***','阿凡達.mp4']

threads =

files = range(len(list))

#建立執行緒

if __name__ == '__main__':

#啟動執行緒

for i in files:

threads[i].start()

for i in files:

threads[i].join()

#主線程

print 'end:%s' %ctime()

效果:

本文永久更新鏈結位址

多執行緒是同時執行多個執行緒的嗎

相信多執行緒各位大佬都用過,不管是在單核cpu還是多核cpu上都可以執行,但是多執行緒是同時執行多個執行緒的嗎?併發和並行 併發 解釋1 當有多個執行緒在操作時,如果系統只有乙個cpu,則它根本不可能真正同時進行乙個以上的執行緒,它只能把cpu執行時間劃分成若干個時間段,再將時間 段分配給各個執行緒...

兩個執行緒同時執行案例

兩個案例同時執行案例 1 這個兩個執行緒並不是有規律的執行而是有沒有規律的交替執行 package com.test3 public class demo10 3 列印 class pig implements runnable public void run catch exception e t...

Python 定時執行執行緒 執行任務

最常用的是以下的方法。不過缺點是sleep函式堵塞了主線程,會造成無妨同時執行其他的程式。usr bin env python encoding utf 8 import time deftask print task deftimer n while true print time.strftim...