python3中怎樣控制線程開始

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#encoding:utf8

import threading
import time

data = 0

def func(sleeptime):
global data
print threading.currentThread().getName()
time.sleep(sleeptime)
threads = []

for i in range(0,40):
t = threading.Thread(target=func,args=(i,))
threads.append(t)

num = 0
for t in threads:
t.start()
while True:
#判斷正在運行的線程數量,如果小於5則退出while循環,
#進入for循環啟動新的進程.否則就一版直在while循環進權入死循環
if(len(threading.enumerate()) < 5):
break

❷ python3.4.還用thread線程嗎

和版本無關。
都用的。
一般計算密集型的使用用進程 process
IO 密集型的可以用線程 thread,當然也可以用進程

❸ python3 寫的線程為什麼被阻塞了

能是僅有的支持多線程的解釋型語言(perl的多線程是殘疾,PHP沒有多線程),Python的多線程是有compromise的,在任意時間只有一個Python解釋器在解釋Python bytecode。

UPDATE:如評論指出,Ruby也是有thread支持的,而且至少Ruby MRI是有GIL的。

如果你的代碼是CPU密集型,多

❹ python中的 thread 模塊執行不正確,請大神幫忙看下代碼哪裡有問題,以及錯誤原因

你調用的是低級的_thread庫,所有的start_new_thread前都該是_thread.不是thread.

❺ Python3.2 threading模塊問題

1) t1和t2要分別join()一下啊
2)別用IDLE的那個環境,即別用F5運行;用cmd運行你這個腳本,或者雙擊你這個腳本運行都行;

❻ python3多線程可以節省多少時間

'''''
Created on 2016年3月6日
@author: Administrator
'''
from time import ctime, sleep
from threading import Thread
def loop0():
print('start loop 0 at :',ctime())
sleep(4)
print('loop 0 at :',ctime())
def loop1():
print('start loop 1 at:',ctime())
sleep(2)
print('loop 1 done at:',ctime())
def main():
print('starting at:',ctime())
t1=Thread(target=loop0,name='loop0')
t2=Thread(target=loop1,name='loop1')
t1.start()
t2.start()
print('all done at:',ctime())
if __name__=='__main__':
main()

❼ python怎麼判斷線程的狀態

is_alive():
Return whether the thread is alive.

http://docs.python.org/3/library/threading.html#threading.Thread.is_alive

❽ python 如何設置threading.thread線程數量

ssh是指向網路命令,肯定要收到帶寬、伺服器允許最大連接數之類的影響,不是想開多少就多少
你換成別的命令來試試,所以很大可能不是python或者線程的原因

❾ python多線程thread.start_new_thread傳參的問題

因為thread.start_new_thread(ssh_cmd,(3,))開的線程會和主線程一起結束,所以等不到執行print number 程序就結束了

❿ 急:python我在調用thread.start_new_thread時發生了如下錯誤,請問是為什麼

這是因為你在start_new_thread里的參數設置錯誤了,你要傳函數名,而不是執行函數

下面給你個例子看看

#!/usr/bin/python

importthread
importtime

#Defineafunctionforthethread
defprint_time(threadName,delay):
count=0
whilecount<5:
time.sleep(delay)
count+=1
print"%s:%s"%(threadName,time.ctime(time.time()))

#Createtwothreadsasfollows
try:
thread.start_new_thread(print_time,("Thread-1",2,))
thread.start_new_thread(print_time,("Thread-2",4,))
except:
print"Error:unabletostartthread"

while1:
pass

看到了嗎,上面的start_new_thread的第一個參數傳的是函數名字