for in會(huì)把[__ob__: Observer]當(dāng)成一個(gè)子項(xiàng)進(jìn)行遍歷,所以res.data.list長(zhǎng)度不是4而是5。
解決方法:
1、for in 里面過(guò)濾掉:
for(let i in obj){
if(typeof obj[i] == "undefined") return;
}
2、不要使用for in 方法,改為:
for(let i = 0 ; i < obj.length; i++){
}#coding:utf-8
import traceback
try:
1/0
except Exception as e:
exception = traceback.format_exc()
print("錯(cuò)誤為:\n----\n{}\n----".format(exception))
輸出結(jié)果:
錯(cuò)誤為:
----
Traceback (most recent call last):
File "file.py", line 5, in <module>
1/0
ZeroDivisionError: integer division or modulo by zero
----
[Finished in 0.1s]
我也剛看,試了好久,初步知道怎么回事了.
先回答你的兩個(gè)問(wèn)題:
1、setDaemon所綁定的都是主線程,都是一樣的,即運(yùn)行py文件第一次創(chuàng)建的那個(gè)線程(也是主進(jìn)程),有且只有一個(gè)
2、queue.join()如果單獨(dú)使用會(huì)被無(wú)限掛起,字面翻譯為等待隊(duì)列為空,再執(zhí)行別的操作.但是他等待的隊(duì)列不是我們創(chuàng)建的aqueue,而是一個(gè)與aqueue長(zhǎng)度相等的一個(gè)"需要完成的子線程"隊(duì)列,他判斷的很可能是這個(gè)列表,當(dāng)這個(gè)列表為空時(shí)queue.join()才成立,所以如樓上的那位所說(shuō)queue.join()要結(jié)合aqueue.task_done()函數(shù)來(lái)使用,aqueue.task_done()的意思是每task_done一次 就從"需要完成的子線程"隊(duì)列里刪掉一個(gè)元素,這樣在最后join的時(shí)候根據(jù)隊(duì)列長(zhǎng)度是否為零來(lái)判斷隊(duì)列是否結(jié)束,aqueue.task_done()用在子線程函數(shù)中,這樣queue.join()才知道完成了多少個(gè)子線程,才不會(huì)無(wú)限掛起,也就是為什么你沒(méi)退出的原因
但是你的程序也有問(wèn)題:
主要的問(wèn)題是,守護(hù)線程機(jī)制:主線程運(yùn)行完畢去結(jié)束子線程時(shí),由于有大量的子線程還在time.sleep(5),結(jié)束這些子線程會(huì)觸發(fā)異常:
Fatal Python error: could not acquire lock for <_io.BufferedWriter name='<stdout>'> at interpreter shutdown, possibly due to daemon threads
Thread 0x000019cc (most recent call first):
File "test.py", line 13 in worker
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864 in run
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916 in _bootstrap_inner
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 884 in _bootstrap
這是因?yàn)?
下面的一些說(shuō)法是錯(cuò)誤的,可以看我后面的補(bǔ)充,這里就不改了,是一種猜測(cè),但是應(yīng)該確實(shí)不是這樣
喂不飽的子線程:你aqueue.put(i)了是10個(gè)元素,但是卻運(yùn)行了20個(gè)子線程來(lái)"吃"這10個(gè)元素,又因?yàn)檫@20個(gè)線程都是守護(hù)進(jìn)程,當(dāng)10個(gè)吃飽了,這個(gè)10個(gè)卡在了item = aqueue.get()等待接受元素,另外的那些很有可能卡在了time.sleep(5)上,而主線程運(yùn)行完畢,準(zhǔn)備結(jié)束所有的守護(hù)進(jìn)程,我判斷,結(jié)束卡在item = aqueue.get()的守護(hù)進(jìn)程不會(huì)報(bào)錯(cuò),而結(jié)束其他程序,如time.sleep會(huì)觸發(fā)異常,這也是為啥樓上那位吧你的for i in range(20):改成了for i in range(1):的原因.
還有兩種退出的方式:
1、是在子線程里判斷隊(duì)列是否為空:
if aqueue.empty() == True:
break
為空就退出
2、主線程通過(guò)queue向子線程發(fā)送退出命令
其實(shí)這里一般用pipe管道的,Queue隊(duì)列不好針對(duì)特定的線程進(jìn)行退出操作,大概的偽碼為:
主線程:
aqueue.put('quit')
子線程""
item = aqueue.get()
if item == 'quit':
return
else:
# "do something"
所以多進(jìn)程,多線程還是要控制好程序邏輯,控制好調(diào)度.
我沒(méi)用過(guò)守護(hù)進(jìn)程,我一般用的是普通的用戶進(jìn)程,這樣主線程跑完會(huì)等待所有子線程運(yùn)行完畢再結(jié)束
其實(shí)還沒(méi)完=_=:
后面的都是我的猜測(cè),算是附加的,選修:
這程序的細(xì)節(jié)也值得我們關(guān)注:
這程序如果真去實(shí)踐的話很容易炸,以下都是我的猜測(cè)
如上所述,我們修改程序,注意注釋
import queue
import threading
import time
aqueue = queue.Queue()
# 發(fā)送了0到9的十個(gè)數(shù)據(jù)
for i in range(10):
aqueue.put(i)
def worker():
while True:
# 輸出當(dāng)前線程,和主線程(可以看到主線程都一樣)
print('thread %s is running...\nmain_thread %s is running...\n\n' % (threading.current_thread().name,threading.main_thread()))
time.sleep(1)
item = aqueue.get()
print(item)
aqueue.task_done()
print('thread end')
n = 1
"""注意這!!!!!!!!!!!!!!!!!!"""
# 運(yùn)行了10個(gè)線程來(lái)處理這十個(gè)數(shù)據(jù)
for i in range(10):
print("run thread Counts: ",n)
n += 1
a = threading.Thread(target = worker)
a.setDaemon(True)
a.start()
aqueue.join()
print('after to do')
print('end')
10個(gè)數(shù)據(jù)10個(gè)線程,time.cleep(5)秒結(jié)果會(huì)怎么樣?運(yùn)行正常?
很不幸運(yùn)的炸了23333333
有一定的幾率報(bào)錯(cuò),和上面一樣:
Fatal Python error: could not acquire lock for <_io.BufferedWriter name='<stdout>'> at interpreter shutdown, possibly due to daemon threads
Thread 0x0000063c (most recent call first):
File "test.py", line 13 in worker
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864 in run
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916 in _bootstrap_inner
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 884 in _bootstrap
我電腦上大部分運(yùn)行正常,只有幾次會(huì)這樣,為什么會(huì)這樣?
換成10個(gè)數(shù)據(jù)9個(gè)線程就不會(huì)出錯(cuò)了
我猜測(cè)的情況是,初步認(rèn)為:
直接看完整的輸出,注意看有注釋的地方:
run thread Counts: 1
thread Thread-1 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
run thread Counts: 2
thread Thread-2 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
run thread Counts: 3
thread Thread-3 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
run thread Counts: 4
thread Thread-4 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
run thread Counts: 5
thread Thread-5 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
run thread Counts: 6
thread Thread-6 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
run thread Counts: 7
thread Thread-7 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
run thread Counts: 8
thread Thread-8 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
run thread Counts: 9
thread Thread-9 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
run thread Counts: 10
thread Thread-10 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
0
1
3
4
thread end
thread end
5
2
thread Thread-2 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
thread end
thread end
thread end
thread Thread-3 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
thread end
thread Thread-4 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
thread Thread-1 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
thread Thread-5 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
6
8
thread Thread-6 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
thread end
thread end
thread Thread-7 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
7
9
thread Thread-9 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
"""到這里主線程已經(jīng)完畢"""
thread end
after to do
"""
這里其實(shí)系統(tǒng)在調(diào)用守護(hù)線程的結(jié)束程序來(lái)結(jié)束所有子線程
"""
"""但是在結(jié)束守護(hù)進(jìn)程程序后,又運(yùn)行了一個(gè)不該運(yùn)行的子線程,這就是為什么會(huì)報(bào)錯(cuò)的原因"""
thread Thread-10 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
end
thread end
thread Thread-8 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
Fatal Python error: could not acquire lock for <_io.BufferedWriter name='<stdout>'> at interpreter shutdown, possibly due to daemon threads
Thread 0x00000424 (most recent call first):
File "test.py", line 14 in worker
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864 in run
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916 in _bootstrap_inner
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 884 in _bootstrap
Thread 0x00000f04 (most recent call first):
File "test.py", line 14 in worker
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864 in run
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916 in _bootstrap_inner
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 884 in _bootstrap
Thread 0x0000165c (most recent call first):
File "test.py", line 13 in worker
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864 in run
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916 in _bootstrap_inner
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 884 in _bootstrap
Thread 0x000018b0 (most recent call first):
File "test.py", line 14 in worker
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864 in run
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916 in _bootstrap_inner
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 884 in _bootstrap
Thread 0x00000db4 (most recent call first):
File "test.py", line 14 in worker
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864 in run
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916 in _bootstrap_inner
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 884 in _bootstrap
Thread 0x00001a88 (most recent call first):
File "test.py", line 14 in worker
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864 in run
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916 in _bootstrap_inner
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 884 in _bootstrap
Thread 0x0000111c (most recent call first):
File "test.py", line 14 in worker
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864 in run
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916 in _bootstrap_inner
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 884 in _bootstrap
Thread 0x0000177c (most recent call first):
File "test.py", line 14 in worker
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864 in run
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916 in _bootstrap_inner
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 884 in _bootstrap
Thread 0x000008e4 (most recent call first):
File "test.py", line 14 in worker
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864 in run
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916 in _bootstrap_inner
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 884 in _bootstrap
Thread 0x00001788 (most recent call first):
File "test.py", line 14 in worker
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864 in run
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916 in _bootstrap_inner
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 884 in _bootstrap
Current thread 0x00001a18 (most recent call first):
看主要的幾句
"""到這里主線程已經(jīng)完畢"""
thread end
after to do
"""
這里其實(shí)系統(tǒng)在調(diào)用守護(hù)線程的結(jié)束程序來(lái)結(jié)束所有子線程
"""
"""但是在結(jié)束守護(hù)進(jìn)程程序后,又運(yùn)行了一個(gè)不該運(yùn)行的子線程,這就是為什么會(huì)報(bào)錯(cuò)的原因"""
thread Thread-10 is running...
main_thread <_MainThread(MainThread, started 6680)> is running...
end
thread end
先這樣吧,可以自己運(yùn)行下程序看看
看了另一個(gè)答主的,使用了a.join()也是一種選擇
改成發(fā)送10個(gè)數(shù)據(jù),用9個(gè)線程跑,效率是最高的(理論)
import queue
import threading
import time
aqueue = queue.Queue()
# 發(fā)送了0到9的十個(gè)數(shù)據(jù)
for i in range(10):
aqueue.put(i)
def worker():
while True:
# 輸出當(dāng)前線程,和主線程(可以看到主線程都一樣)
print('thread %s is running...\nmain_thread %s is running...\n\n' % (threading.current_thread().name,threading.main_thread()))
time.sleep(5)
item = aqueue.get()
print(item)
aqueue.task_done()
print('thread end')
n = 1
"""注意這!!!!!!!!!!!!!!!!!!"""
for i in range(9):
print("run thread Counts: ",n)
n += 1
a = threading.Thread(target = worker)
a.setDaemon(True)
a.start()
aqueue.join()
print('after to do')
print('end')
你的django項(xiàng)目是2.7的項(xiàng)目吧?
或者是你服務(wù)器上有倆個(gè)版本的Python ?
python3 manage.py runserver
python 的文檔:https://docs.python.org/2/lib... 顯示:
The built-in sorted() function is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade).
即,不改變比較相等的元素的相對(duì)位置。根據(jù)問(wèn)題給出的 key,237 和 100 是“相等”的,所以它們之間的相對(duì)關(guān)系不會(huì)變化,即 237 在 100 之前。
jsonify封裝的內(nèi)部指定了content_type為application/json(mimetype參數(shù))。
python代碼需要增加一行
import time
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
if __name__ == "__main__":
#client_id是必須的,并且是唯一的。否則可能會(huì)出現(xiàn)如下錯(cuò)誤
client_id = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
client = mqtt.Client(client_id) #ClientId不能重復(fù),所以使用當(dāng)前時(shí)間
client.username_pw_set(username="admins",password="password")
client.on_connect = on_connect
client.connect("127.0.0.1", 61613, 60)
client.loop_start()
#while True:
time.sleep(1)
client.publish(topic="message", payload="hello")Django models支持abstract=True屬性, 設(shè)置這個(gè)屬性后, 這個(gè)models不會(huì)在創(chuàng)建表, 專門用來(lái)繼承, 具體的可以看官方文檔 Models Abstract base classes部分.
Abstract base classes
Abstract base classes are useful when you want to put some common information into a number of other models. You write your base class and put abstract=True in the Meta class. This model will then not be used to create any database table. Instead, when it is used as a base class for other models, its fields will be added to those of the child class. It is an error to have fields in the abstract base class with the same name as those in the child (and Django will raise an exception).
from django.db import models
class CommonInfo(models.Model):
name = models.CharField(max_length=100)
age = models.PositiveIntegerField()
class Meta:
abstract = True
class Student(CommonInfo):
home_group = models.CharField(max_length=5)
The Student model will have three fields: name, age and home_group. The CommonInfo model cannot be used as a normal Django model, since it is an abstract base class. It does not generate a database table or have a manager, and cannot be instantiated or saved directly.
pip install --upgrade matplotlib
stackoverflow:python mpl_toolkits installation issue
百度搜索python GIL
用開發(fā)者工具看看請(qǐng)求Request Payload
np.array(im)的數(shù)據(jù)跟arr數(shù)據(jù)都不一致
Image.fromarray(arr,'RGB')的問(wèn)題
arr = np.random.randint(0,256**4 - 1,(100,100), dtype='uint32') #無(wú)符號(hào)int 4字節(jié)
im = Image.fromarray(arr,'RGBA') #按字節(jié)讀取
im.show()
:height="tableHeight"
mounted(){
this.tableHeight=700;
}
這種方式是對(duì)的呀 運(yùn)行出來(lái)就是700
這個(gè)還要怎么封裝?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
print('我愛(ài)魚C工作室........................')
import random
secret=random.randint(1,10)
temp=input('猜猜小甲魚心里想的是數(shù)字幾:')
count=3
while count:
while not temp.isdigit():
temp=input('這不合法,請(qǐng)輸入一個(gè)合法整數(shù):')
guess = int(temp)
if guess==secret:
print('猜對(duì)了,干的漂亮')
break
elif guess>secret:
print('大了大了',end='')
else:
print('小了小了',end='')
count-=1
print(count)
print('你還有%d次機(jī)會(huì),請(qǐng)輸入:'%count,end=' ')
guess=int(input())
if count==1:
print('你的次數(shù)已經(jīng)用完,不玩了。')
print('小甲魚心里想的是數(shù)字%d'%secret,end=' ')
break
猜猜小甲魚心里想的是數(shù)字幾:7
2
你還有2次機(jī)會(huì),請(qǐng)輸入: 9
1
你還有1次機(jī)會(huì),請(qǐng)輸入: 2
你的次數(shù)已經(jīng)用完,不玩了。
小甲魚心里想的是數(shù)字3
寫個(gè)腳本自動(dòng)啟動(dòng)運(yùn)行,就可以
for line in f:
是在調(diào)用 f.next().
f.next() 方法被調(diào)用的時(shí)候,f.tell() 方法不可以被調(diào)用
如果需要返回當(dāng)前指針位置,可以這樣
pos = 0
with open(r'My Daily', 'r+') as f:
for line in f:
pos += len(lien)
#print f.tell()
print(post)打斷點(diǎn)
F12開發(fā)者工具-sources
可以設(shè)置停止的點(diǎn)
執(zhí)行到你設(shè)置的停止點(diǎn),就中斷了。
將點(diǎn)設(shè)置到彈窗關(guān)閉前那句
具體設(shè)置方法,是在想要設(shè)置的那一行行數(shù)上點(diǎn)擊一下
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
北大課工場(chǎng)是北京大學(xué)校辦產(chǎn)業(yè)為響應(yīng)國(guó)家深化產(chǎn)教融合/校企合作的政策,積極推進(jìn)“中國(guó)制造2025”,實(shí)現(xiàn)中華民族偉大復(fù)興的升級(jí)產(chǎn)業(yè)鏈。利用北京大學(xué)優(yōu)質(zhì)教育資源及背
博為峰,中國(guó)職業(yè)人才培訓(xùn)領(lǐng)域的先行者
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。