我也剛看,試了好久,初步知道怎么回事了.
先回答你的兩個(gè)問題:
1、setDaemon所綁定的都是主線程,都是一樣的,即運(yùn)行py文件第一次創(chuàng)建的那個(gè)線程(也是主進(jìn)程),有且只有一個(gè)
2、queue.join()如果單獨(dú)使用會(huì)被無限掛起,字面翻譯為等待隊(duì)列為空,再執(zhí)行別的操作.但是他等待的隊(duì)列不是我們創(chuàng)建的aqueue,而是一個(gè)與aqueue長(zhǎng)度相等的一個(gè)"需要完成的子線程"隊(duì)列,他判斷的很可能是這個(gè)列表,當(dāng)這個(gè)列表為空時(shí)queue.join()才成立,所以如樓上的那位所說queue.join()要結(jié)合aqueue.task_done()函數(shù)來使用,aqueue.task_done()的意思是每task_done一次 就從"需要完成的子線程"隊(duì)列里刪掉一個(gè)元素,這樣在最后join的時(shí)候根據(jù)隊(duì)列長(zhǎng)度是否為零來判斷隊(duì)列是否結(jié)束,aqueue.task_done()用在子線程函數(shù)中,這樣queue.join()才知道完成了多少個(gè)子線程,才不會(huì)無限掛起,也就是為什么你沒退出的原因
但是你的程序也有問題:
主要的問題是,守護(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)?
下面的一些說法是錯(cuò)誤的,可以看我后面的補(bǔ)充,這里就不改了,是一種猜測(cè),但是應(yīng)該確實(shí)不是這樣
喂不飽的子線程:你aqueue.put(i)了是10個(gè)元素,但是卻運(yùn)行了20個(gè)子線程來"吃"這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、主線程通過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)度.
我沒用過守護(hù)進(jìn)程,我一般用的是普通的用戶進(jìn)程,這樣主線程跑完會(huì)等待所有子線程運(yùn)行完畢再結(jié)束
其實(shí)還沒完=_=:
后面的都是我的猜測(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è)線程來處理這十個(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é)束程序來結(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é)束程序來結(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')
文檔里有寫啊
https://zh.nuxtjs.org/api
asyncData 可以接受一個(gè) context 參數(shù),包含了當(dāng)前環(huán)境所有信息
context.store 就能獲取到 vuex 實(shí)例了,具體內(nèi)容打開鏈接進(jìn)去看吧
跟你5px的scrollbar 有關(guān)。頁(yè)面比較高的時(shí)候,scrollbar會(huì)占個(gè)位置。
大概思路:
crontab里面添加這個(gè)xxx.py的文件,或者用supervisor管理這個(gè)進(jìn)程
然后xxx.py里面執(zhí)行subprocess.call('http-server')
不是很嚴(yán)謹(jǐn),但這個(gè)思路能實(shí)現(xiàn)
看了一下你的鏈接,發(fā)現(xiàn)后面就是你想附帶的參數(shù)
想確認(rèn)一下你是準(zhǔn)備用get帶params
或者還是post提交data
確認(rèn)一下
requests.get(url=url,params=data)
#
requests.post(url=url,data=data)view.py中把網(wǎng)頁(yè)表單賦值完后info_form.save()才算保存
npm install -g vue-cli@版本號(hào)
查看版本號(hào)
vue -V這個(gè)可以用shapes做,具體可以看看張?chǎng)涡竦倪@篇文章
另外附注:
這個(gè)也可以用已經(jīng)被廢棄的屬性region做到這個(gè)效果
詳細(xì)請(qǐng)百度,不過已經(jīng)被chrome廢棄了
應(yīng)該把 elementPathEnabled 設(shè)置為 true 吧
試一下SQLAlchemy的event吧(http://docs.sqlalchemy.org/en...)
一個(gè)簡(jiǎn)單的使用例子
import sqlalchemy
from sqlalchemy import event
from models import Wallet
class User(db.Model):
name = sqlalchemy.column(s.String)
@staticmethod
def after_create(mapper, connection, target):
wallet = Wallet()
db.session.add(wallet)
db.session.commit()
event.listen(User, 'after_insert', User.after_create)crx后綴改成zip解壓。。。
const $ = document.querySelector.bind(document)不用格式化輸入,Python查詢數(shù)據(jù)庫(kù)有相應(yīng)的庫(kù),例如:https://pypi.python.org/pypi/...,安裝之后查詢的方法查看官方文檔:https://dev.mysql.com/doc/con...
v-for里面不需要寫括號(hào)的,然后就是樓上兄弟說的打印少寫了一半引號(hào)。其他貌似沒有什么問題
自問自答。
module = __import__('module.%s' % modulename)
class_ = module()
func = getattr(class_, 'func')
func()不知道是不是你想要的
我覺得與樓主提供的的連接相比,不如分成面向?qū)ο?/code>、面向過程,以及函數(shù)式編程。這里顯然是面向?qū)ο蟮摹?/p>
一個(gè) class 影響 "input-group"
你看下兩個(gè)請(qǐng)求的請(qǐng)求頭是不是一樣的呢?
關(guān)注下瀏覽器中的請(qǐng)求頭,304會(huì)有:If-Modified-Since 和 If-None-Match(緩存機(jī)制)。然后Chrome調(diào)試模式中可以僅用緩存,你再刷新請(qǐng)求試試呢
pycharm 寫python不錯(cuò),錯(cuò)誤提示也很友好
北大青鳥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ù)顧問,美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。