import subprocess
cmd = subprocess.Popen('tracert -d', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
for info in iter(cmd.stdout.readline, 'b'):
    if info == b'':
        print(str(info, encoding='gbk'))

python代码如上,保存在test.py中,在Linux中执行python 一直没有反应,直接ctrl后提示错误:

^CTraceback (most recent call last):
  File "", line 4, in <module>
    for info in iter(cmd.stdout.readline, 'b'):


我好菜为啥还被占用了我最菜 你好,我也遇到了同样的问题,请问你的问题最终是怎么解决的?感谢!
18 天之前 回复
Csdn user default icon
python 在进行机器学习时,没有键盘输入ctrl C但是出现KeyboardInterrupt错误,这是什么原因呢?

1.在运行中并没有键盘输入ctrl C,运行一段时间后,出现: 2. 错误提示: ``` Traceback (most recent call last): File "F:\new\new world\deep learning\CNN_ Batch_normalization\CNN_Batch_normalization\", line 101, in <module> trainer.train() File "..\common\", line 80, in train self.train_step() File "..\common\", line 53, in train_step grads =, t_batch) File "F:\new\new world\deep learning\CNN_ Batch_normalization\CNN_Batch_normalization\", line 127, in gradient dout = layer.backward(dout) File "..\common\", line 176, in backward dx = self.__backward(dout) File "..\common\", line 185, in __backward dxc = dxn / self.std KeyboardInterrupt ``` 不清楚是什么原因,望大佬解答!!!

Python how to run many case in batch

Python 自动化运行批量case,如何做到像windows批处理? 请高手赐教,多谢 例如 case.list include python case.list ---try to do like this


<div class="post-text" itemprop="text"> <p>I am new to Python, but I want to write a multiproccesing program that uses <code>multiprocessing.Queue</code> as Go's channel. However, it seems that <code>multiprocessing.Queue.put(, block=True)</code> doesn't block as <a href="" rel="nofollow noreferrer">documented</a>.</p> <p>Here is my Python example program:</p> <pre><code>import multiprocessing import select import time def f(q): while True: time.sleep(1) print("Put") q.put("hello world") if __name__ == "__main__": q = multiprocessing.Queue(maxsize=0) f(q) </code></pre> <p>I expect that it will print only one "Put" and blocks forever. However, what I got is that it prints "Put" repeatedly.</p> <p>Here is the Go program I think almost equivalent, except that goroutine is a green thread but not a process.</p> <pre><code>package main import ( "fmt" "time" ) func main() { ch := make(chan int) for { time.Sleep(time.Second) fmt.Println("Put") ch &lt;- 1 } } </code></pre> <p>When I type <code>go run csp.go</code>, it prints only one "Put" and the Go runtime prompts that the program is in deadlock.</p> <p>Why <code>multiprocessing.Queue.put()</code> doesn't block?</p> </div>


我正在做一个项目其中运用了神经棒,我想用运用多进程将神经棒与另外一个程序进行通讯 ,但是我发现用多进程的时候,他总是显示无法开启神经棒不知道是什么原因。有没有大神 能帮忙解决下。 主程序如下: ** from move import Move from hh import Detect from capture1 import Capture from multiprocessing import Process, Queue import time import cv2 q1 = Queue() q2 = Queue() def CAP_DETC(q1,q2): cap = Capture() cap_detc = Detect() while True: cap.save_img() img = cv2.imread('images/1.jpg') time.sleep(0.1) if q2.get(): p = cap_detc.preprocess_image(img) m = cap_detc.predict(p) n = cap_detc.judge(m) q1.put(n) def MOVE(q1,q2): move = Move() q2.put(1) while True: if q1.get() == 1: move.turn_right() elif q1.get() == 2: move.turn_left() elif q1.get() == 0: move.go_forward() elif q1.get() == 3: move.stop() move.turn_left() else : print 'Something is wrong!' move.stop() p1 = Process(target=CAP_DETC,args=(q1,q2)) p2 = Process(target=MOVE,args=(q1,q2)) p1.start() p2.start() p1.join() p2.join()** 在Terminal显示的错误: Process Process-1: Traceback (most recent call last): File "/usr/lib/python2.7/multiprocessing/", line 258, in _bootstrap File "/usr/lib/python2.7/multiprocessing/", line 114, in run self._target(*self._args, **self._kwargs) File "", line 13, in CAP_DETC cap_detc = Detect() File "/home/pi/robot/", line 21, in __init__ self.device.OpenDevice() File "/usr/local/lib/python2.7/dist-packages/mvnc/", line 147, in OpenDevice raise Exception(Status(status)) Exception: mvncStatus.ERROR ^CTraceback (most recent call last): File "", line 46, in <module> p2.join() File "/usr/lib/python2.7/multiprocessing/", line 145, in join res = self._popen.wait(timeout) File "/usr/lib/python2.7/multiprocessing/", line 154, in wait return self.poll(0) File "/usr/lib/python2.7/multiprocessing/", line 135, in poll pid, sts = os.waitpid(, flag) KeyboardInterrupt Process Process-2: Traceback (most recent call last): File "/usr/lib/python2.7/multiprocessing/", line 258, in _bootstrap File "/usr/lib/python2.7/multiprocessing/", line 114, in run self._target(*self._args, **self._kwargs) File "", line 27, in MOVE if q1.get() == 1: File "/usr/lib/python2.7/multiprocessing/", line 117, in get res = self._recv() KeyboardInterrupt 如果我采用多线程的方式的话,它是可以进行的。一开启进程就不可以了希望有大神帮忙解决下。


<div class="post-text" itemprop="text"> <p>I want to get a Grpc demo which client is Golang and server is Python. I use Grpc official code as my demo, but it's doesn't work. Can someone help me?</p> <p>If I generate the client and server by same language, example: Python or Golang. It works correctly.</p> <ol> <li>helloworld.proto</li> </ol> <pre><code>syntax = "proto3"; package helloworld; // The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} } // The request message containing the user's name. message HelloRequest { string name = 1; } // The response message containing the greetings message HelloReply { string message = 1; } </code></pre> <p>2.get relevant code</p> <p>1) get python relevant code: python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. helloworld.proto</p> <p>2) get go relevant code: protoc -I . helloworld.proto --go_out=plugins=grpc:.</p> <ol start="3"> <li></li> </ol> <pre class="lang-py prettyprint-override"><code>import time import logging import grpc from concurrent import futures import helloworld_pb2 import helloworld_pb2_grpc _ONE_DAY_IN_SECONDS = 60 * 60 * 24 class Greeter(helloworld_pb2_grpc.GreeterServicer): def SayHello(self, request, context): return helloworld_pb2.HelloReply(message='Hello, %s!' % def serve(): server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) server.add_insecure_port('[::]:50051') server.start() try: while True: time.sleep(_ONE_DAY_IN_SECONDS) except KeyboardInterrupt: server.stop(0) if __name__ == '__main__': logging.basicConfig() serve() </code></pre> <ol start="4"> <li>greeter_client/main.go</li> </ol> <pre class="lang-golang prettyprint-override"><code>package main import ( "context" "log" "os" "time" "" pb "demo/grpc/go_python/helloworld" ) const ( address = "localhost:50051" defaultName = "world" ) func main() { // Set up a connection to the server. conn, err := grpc.Dial(address, grpc.WithInsecure()) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() c := pb.NewGreeterClient(conn) // Contact the server and print out its response. name := defaultName if len(os.Args) &gt; 1 { name = os.Args[1] } ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() r, err := c.SayHello(ctx, &amp;pb.HelloRequest{Name: name}) if err != nil { log.Fatalf("could not greet: %v", err) } log.Printf("Greeting: %s", r.Message) } </code></pre> <ol start="5"> <li>run the demo</li> </ol> <pre><code>server: python client: go run main.go </code></pre> <p>But, I get the wrong output for client:</p> <pre><code>2019/08/07 19:18:03 could not greet: rpc error: code = Unimplemented desc = Method not found! exit status 1 </code></pre> <p>I expect to the demo work.</p> </div>


``` import pyaudio import wave import time def uptime(x): x=time.perf_counter() return x #问题出现在这一部分 def main(): CHUNK = 1024 FILENAME = '1.wav' wf ='1.wav', 'rb') p = pyaudio.PyAudio() stream =,\ channels=wf.getnchannels(),rate=wf.getframerate(),output=True) data = wf.readframes(CHUNK) while data != '': stream.write(data) data = wf.readframes(CHUNK) stream =, channels=chan, rate=rate, input=True, stream_callback=callback) stream.stop_stream() stream.close() p.terminate() return i=0 while i<4: t=time.perf_counter() x=t while uptime(x)<t+3: x=time.perf_counter() i=i+1 print(i) main() ``` 系统报错: Traceback (most recent call last): File "D:\python文件\自\计时器.py", line 41, in <module> main() File "D:\python文件\自\计时器.py", line 21, in main stream.write(data) File "C:\Users\xlw\AppData\Local\Programs\Python\Python37-32\lib\site-packages\", line 586, in write exception_on_underflow) KeyboardInterrupt


import serial if __name__ == '__main__': serial = serial.Serial('COM3',115200) if serial.isOpen(): print('串口打开成功!\n') f = open('./test.txt','a') print(1) #pass else : print('串口打开失败!\n') try: getBytes=b'' while True: count = serial.inWaiting() if count > 0: data = print('数据读取成功') if data != getBytes: print(data) f.write(data.decode('utf-8')) f.write('\n') getBytes=data except KeyboardInterrupt: if serial != None: f.close() serial.close()

使用Raspberry GPIO的Python和PHP

<div class="post-text" itemprop="text"> <p>With a little Python script I'm able to detect a button state that is connected in my Raspberry board. This is the script:</p> <pre><code>#!/usr/bin/env python import os import RPi.GPIO as GPIO import time GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) try: while True: if(GPIO.input(12) == 1): print 'Btn on' os.system('date') time.sleep(5) else: os.system('clear') print 'Waiting' time.sleep(2) except KeyboardInterrupt: GPIO.cleanup() </code></pre> <p>In this way, using PuTTY, or directly in the Raspberry terminal, I can easily detect the state of this button. </p> <p>Now I would create the same thing but in PHP. I need to create a PHP web page that will be in the <code>var/www/html</code> directory of my Raspberry, and should just show me the same thing. When I press the button, I will get a simple echo "Btn on" with the timestamp, and if not pressed just another echo with "Waiting".. Is it possible? I tried directly <code>exec</code> the Python script, in PHP, in this way:</p> <pre><code>$command = escapeshellcmd('sudo -u www-data python'); $output = shell_exec($command); echo $output; </code></pre> <p>but it's not working. Any idea?</p> </div>


我使用while 1循环读取数据库的一个表内容,当内容改变时,无法实时改变,求大神帮我看下代码 import MySQLdb import serial import time ser = serial.Serial('/dev/ttyACM0',9600,timeout=1) conn=MySQLdb.connect(host="",user="root",passwd="123456",db="test") cursor = conn.cursor() try: while 1: time.sleep(1.5) n = cursor.execute("select state from t_led") r = cursor.fetchone() p = r[0] ser.write(p) a = ser.readline() print r except KeyboardInterrupt: conn.close() ser.close() cursor.close()


<div class="post-text" itemprop="text"> <p>I have a python script which uses pyaudio to record audio using microphone and the code converts speech to text.It works perfectly fine on terminal. Here is a section of the python code :</p> <pre><code>stream =, channels=CHANNELS, rate=RATE, input=True, output=True, frames_per_buffer=CHUNK) async def send_audio(ws): # Starts recording of microphone print("* READY *") start = time.time() while True: try: #print(".") data = await ws.send(data) ##if time.time() - start &gt; 30: # Records for n seconds ##await ws.send(json.dumps({'action': 'stop'})) ##return False except Exception as e: print(e) return False except KeyboardInterrupt: # allow Ctrl + C to shut down the program await ws.send(json.dumps({'action': 'stop'})) return False # Stop the stream and terminate the recording stream.stop_stream() stream.close() p.terminate() async def speech_to_text(): async with websockets.connect(url, extra_headers=token_header) as conn: # Send request to watson and waits for the listening response send = await conn.send(json.dumps(params)) rec = await conn.recv() print(rec) asyncio.ensure_future(send_audio(conn)) w=0 global lst # Keeps receiving transcript until we have the final transcript while True: try: rec = await conn.recv() parsed = json.loads(rec) transcript = parsed["results"][0]["alternatives"][0]["transcript"] #print(parsed) if "results" in parsed: if len(parsed["results"]) &gt; 0: if "final" in parsed["results"][0]: if parsed["results"][0]["final"]: #conn.close() #return False print(transcript) lst.append(transcript) lst.append(" ") pass except KeyError: conn.close() return False # Starts the application loop loop = asyncio.get_event_loop() loop.run_until_complete(speech_to_text()) loop.close() </code></pre> <p>Now,I want to call this script in a web page by clicking a button.I tried using exec() and shell_exec() in php as:</p> <pre><code>&lt;div&gt; &lt;?php echo "&lt;h3&gt;POST:&lt;/h3&gt;"; print_r($_POST); if(isset($_POST["submit"])){ print_r (shell_exec("cd / &amp;&amp; python C:/xampp/htdocs/heyy/")); } ?&gt; &lt;form method="post" action="try.php "&gt; &lt;input type="submit" name="submit" value="Submit"&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p>But on clicking the button,the page is stuck on loading screen and gives no response.If I replace "" with simple "" printing hello world,it works.Any idea where the problem is ? or any better ways to solve this?</p> </div>


<div class="post-text" itemprop="text"> <p>I have read through a bunch of forums and still no luck.. I have a php file that executes a python file ( It runs the py file once and stops, how to I keep the file open in the background? If I run sudo python in the command line it just runs once as well but if I put sudo python -i it stays open. How to I do this in php?</p> <p>php file - </p> <pre><code>&lt;?php $try = exec('python'); print_r ($try); ?&gt; </code></pre> <p>In the file I have 2 vibration sensors that are being detected and then passing that info into sqlite, then my php file retrieves that data and using ajax I can display it. All of this works great except launching the py file continually..</p> <p>py script - </p> <pre><code>#!/usr/bin/env python import RPi.GPIO as GPIO import time import sqlite3 KnockPin = 12 ShockPin = 13 inning = 1 runs = 0 strikes = 0 balls = 0 print ("Welcome to Bases Loaded") def setstr(): global strikes strikes = 0 global balls balls = 0 def setup(): GPIO.setmode(GPIO.BOARD) GPIO.setup(KnockPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(ShockPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) def knock(ev=None): with sqlite3.connect('basesLoaded.db') as conn: global strikes global inning strikes += 1 if strikes ==0: pass elif strikes == 1: conn.execute("UPDATE bl set STRIKE = 1 WHERE ID =1"); print ("Strike 1 :", conn.total_changes); elif strikes == 2: conn.execute("UPDATE bl set STRIKE = 2 WHERE ID=1"); print ("Strike 2 :", conn.total_changes); elif strikes == 3: global inning inning +=1 conn.execute("UPDATE bl set STRIKE = 0 WHERE ID=1"); conn.execute("UPDATE bl set INNING = inning WHERE ID=1"); print ("Strike 3 Your Out :", conn.total_changes); setstr() print ("Inning Number :", inning); def shock(ev=None): with sqlite3.connect('basesLoaded.db') as conn: global balls balls += 1 if balls ==0: pass elif balls == 1: conn.execute("UPDATE bl set BALL = 1 WHERE ID=1"); conn.commit() print ("Ball 1 :", conn.total_changes); elif balls == 2: conn.execute("UPDATE bl set BALL = 2 WHERE ID=1"); conn.commit() print ("Ball 2 :", conn.total_changes); elif balls == 3: conn.execute("UPDATE bl set BALL = 3 WHERE ID=1"); conn.commit() print ("Ball 3 :", conn.total_changes); elif balls == 4: global runs runs += 1 conn.execute("UPDATE bl set BALL = 0 WHERE ID=1"); conn.execute("UPDATE bl set RUN = 'runs' WHERE ID=1"); conn.commit() print ("Run Scored! score is :", runs); setstr() def register_callbacks(): GPIO.add_event_detect(ShockPin, GPIO.FALLING, callback=shock, bouncetime=2500) GPIO.add_event_detect(KnockPin, GPIO.FALLING, callback=knock, bouncetime=2500) if __name__ == '__main__': try: setup() register_callbacks() except KeyboardInterrupt: destroy() </code></pre> <p>This returns my 'Welcome to Bases Loaded' message in the browser but the py file wont stay open to detect the sensors.</p> </div>

关于python的could not convert string to float的问题。

#!/usr/bin/env python3 #-*- coding: utf-8 -*- import time import subprocess import os from subprocess import call import csv #change offset here offset_peak = 45.0 offset_rsm = 40.0 header_csv = ("time", "amplitude", "rms") try: while True: #pkill because sometimes my microphone was busy"pkill -9 sox | pkill -9 arecord",shell= True) time.sleep( 1 ) #time filedate = time.strftime("%Y%m%d-%H%M%S") filename = "/home/pi/noise/mp3/" + time.strftime("%Y%m%d") + "/" + filedate + ".mp3" filename_csv = "/home/pi/noise/csv/" + time.strftime("%Y%m%d") + ".csv" filedate_csv = time.strftime("%Y-%m-%d %H:%M") terminal_time = time.strftime("%H:%M ") #record"arecord -D hw:1,0 -d 10 -v --fatal-errors --buffer-size=192000 -f dat -t raw --quiet | lame -r --quiet --preset standard - " + filename,shell= True) proc = subprocess.getoutput("sox " + filename + " -n stat 2>&1 | grep 'Maximum amplitude' | cut -d ':' -f 2") proc_rms = subprocess.getoutput("sox " + filename + " -n stat 2>&1 | grep 'RMS.*amplitude' | cut -d ':' -f 2") os.system('clear') proc1 = proc.strip() proc1 = float(proc1) proc_rms = proc_rms.strip() proc_rms = float(proc_rms) #test your microphone in 5 dB steps and create the function e.g. with #Fkt 3 30-80 dB proc3 = 83.83064 + (28.34183 - 83.83064)/(1 + (proc1/0.04589368)**1.006258) #Fkt RMS 30-80 dB proc3_rms = 87.69054 + (23.81973 - 87.69054)/(1 + (proc_rms/0.01197014)**0.7397556) #add db filextentions: peak - rms ext_peak = int(round(proc3, 0)) ext_rms = int(round(proc3_rms, 0)) print("Measured values: " + str(proc1) + " / " + str(proc_rms) + " / " + str(proc3) + " / " + str(proc3_rms) + " / " + str(ext_peak) + "\n") #csv file_exists = os.path.isfile(filename_csv) daten_csv = (filedate_csv, proc3, proc3_rms) with open(filename_csv, 'a', newline='') as f: writer = csv.writer(f) if not file_exists: writer.writerow(header_csv) writer.writerow(daten_csv) if proc3 >= offset_peak or proc3_rms >= offset_rsm: print(terminal_time + "Sound detected - save: " + filedate + ".mp3 \n") os.rename(filename, "/home/pi/noise/mp3/" + time.strftime("%Y%m%d") + "/" + filedate + "-" + str(ext_peak) + "-" + str(ext_rms) + ".mp3") time.sleep( 3 ) #os.system('clear') else: print(terminal_time + "No sound detected, delete: " + filedate + ".mp3 \n") os.remove(filename) time.sleep( 3 ) #os.system('clear') except KeyboardInterrupt:"pkill -9 sox | pkill -9 arecord",shell= True) print('End') 详细代码如上,当我运行的时候不知为何python shell显示错误,错误信息如下: Trackback (most recent call last): File:"/home/pi/noise_level_protocol-master/, line 30 in <module> proc1 = float(proc1) ValueError: could not convert string to float: 希望论坛里的各位大神能够指点迷津, 谢谢。

使用 Python 协程多并发下载 任务数卡住不动

在使用 python 协程下载图片中,最终协程的任务数 卡在 97 一直循环,不知道哪里出了问题,有大佬知道什么情况吗,困扰我好久 下图是我运行结果,附上代码。 ![图片说明]( ``` # -*- coding: utf-8 -*- import requests from lxml import etree import time import os import pandas as pd import asyncio import aiohttp import aiomysql from random import randint import cchardet import aiofiles import logging class sikupicture_Spider(object): def __init__(self): # self.seens_url = [] self.loop = asyncio.get_event_loop() self.queue = asyncio.PriorityQueue() self._workers = 0 # 当前工作数 self._max_workers = 150 # 最大工作数 self.overtime = {} # {url: times,} 记录失败的URL的次数 self.overtime_threshold = 4 self.headers = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36", } self.list_content = [] async def init_url(self): info = pd.read_excel(r"{}".format(os.path.abspath('moban.xlsx'))).fillna('') for ite in info.itertuples(): await self.queue.put((randint(1, 5), getattr(ite, 'url'))) async def fetch(self, session, url, timeout, headers=None, binary=False, proxy=None): _headers = self.headers if headers: _headers = headers try: async with session.get(url, headers=_headers, timeout=timeout, proxy=proxy, allow_redirects=False) as resp: status_code = resp.status if status_code == 403: print("url-403", url) if url in self.overtime: self.overtime[url] += 1 if self.overtime[url] > self.overtime_threshold: pass await self.queue.put((randint(1, 5), url)) else: self.overtime[url] = 1 await self.queue.put((randint(1, 5), url)) status_code = 0 html = None if binary: text = await encoding = cchardet.detect(text) html = text.encode(encoding, errors='ignore') else: html = await resp.text() except TimeoutError: print("url-overtime", url) if url in self.overtime: self.overtime[url] += 1 if self.overtime[url] > self.overtime_threshold: pass await self.queue.put((randint(1, 5), url)) else: self.overtime[url] = 1 await self.queue.put((randint(1, 5), url)) status_code = 0 html = None return status_code, html async def download_img(self, session, img_url, timeout, url, headers=None, binary=True, proxy=None): _headers = self.headers if headers: _headers = headers try: async with session.get(img_url, headers=_headers, timeout=timeout, proxy=proxy, allow_redirects=False) as resp: status_code = resp.status if binary: html = await else: html = await resp.text() except TimeoutError: print("url-overtime", img_url) if url in self.overtime: self.overtime[url] += 1 if self.overtime[url] > self.overtime_threshold: pass else: await self.queue.put((randint(1, 5), url)) else: self.overtime[url] = 1 await self.queue.put((randint(1, 5), url)) status_code = 0 html = None return status_code, html def parse_source(self, source): try: response_1 = etree.HTML(source) except Exception as err: logging.error(f'parse error:{err}') url = "" else: img_url = response_1.xpath("//a[@href='javascript:;']/@supsrc")[0] if len( response_1.xpath("//a[@href='javascript:;']/@supsrc")[0]) else "" return img_url async def process(self, session, url, timeout): status, source = await self.fetch(session, url, timeout) file_name = url.replace("", "").replace(".shtml", "") if status == 200: img_url = self.parse_source(source) img_status, img_source = await self.download_img(session, img_url, timeout, url) if img_status == 200: async with"F:\\dawnzhu\\picture\\"+file_name+".jpg", "wb") as f: await f.write(img_source) self._workers -= 1 print("任务完成", self._workers, "url_status", status, "img_status", img_status) else: self._workers -= 1 print("任务完成", self._workers, "url_status", status,) async def loop_crawl(self): await self.init_url() timeout = aiohttp.ClientTimeout(total=20) conn = aiohttp.TCPConnector(loop=self.loop, limit=50, force_close=True, enable_cleanup_closed=True) session = aiohttp.ClientSession(connector=conn, timeout=timeout) while True: if self._workers >= self._max_workers: print("work 的判断") await asyncio.sleep(5) continue if self.queue.empty(): print("队列是否为空....", self._workers) await asyncio.sleep(5) if self._workers == 0: break continue _, url = await self.queue.get() asyncio.ensure_future(self.process(session, url, timeout)) self._workers += 1 print("队列剩余数量", self.queue.qsize(), self._workers) await session.close() def run(self): try: self.loop.run_until_complete(self.loop_crawl()) except KeyboardInterrupt: self.loop.close() if __name__ == '__main__': sp = sikupicture_Spider() ```

python的NameError: name 'xx‘ is not defined该怎么解决?

初学python 按着书上的代码打 想看看运行过程 代码如下 ``` stack=[] def pushit(): stack:append(input(' Enter New String: ').strip()) def popit(): if len(stack)==0: print('Cannot pop from an empty stack!') else: print ('Removes [','stack.pop()',']') def viewstack(): print(stack) CMDs={'u':pushit,'o':popit,'v':viewstack} def showmenu(): pr=''' p(U)sh p(O)p (V)iew (Q)uit Enter choice:''' while True: while True: try: choice=input(pr).strip()[0].lower() except (EOFError,KeyboardInterrupt,IndexError): choice='q' print('\nYou picked:[%s]'%choice) if choice not in 'uovq': print('Invalid option,try again') else: break if choice=='q': break CMDs[choice]() if _name_=='_main_': showmenu() ``` 但是总提示NameError: name 'pr' is not defined ,该怎么解决这个问题?

python HTMLParser问题

在看python核心编程第三版的时候遇到的问题 ```import io import formatter from html.parser import HTMLParser import http import os import sys import urllib.request from urllib.parse import urlparse,urljoin class Retriever(object): __slots__=('url','file') def __init__(self,url): self.url,self.file=self.get_file(url) def get_file(self,url,default='index.html'): 'Create usable local filename from URL' parsed=urlparse(url) host=parsed.netloc.split('@')[-1].split(':')[0] filepath='%s%s' % (host,parsed.path) if not os.path.splitext(parsed.path)[1]: filepath=os.path.join(filepath,default) linkdir=os.path.dirname(filepath) if not os.path.isdir(linkdir): if os.path.exists(linkdir): os.unlink(linkdir) os.makedirs(linkdir) return url,filepath def download(self): 'Download URL to specific named file' try: retval=urllib.request.urlretrieve(self.url,self.file) except (IOError,httplib.InvalidURL) as e: retval=(('*** ERROR: bad URL "%s": %s' % (self.url, e)),) return retval def parse_links(self): 'Parse out the links found in download HTML file' f=open(self.file,'rb') f.close() parser=HTMLParser(formatter.AbstractFormatter(formatter.DumbWriter(io.StringIO()))) parser.feed(data) parser.close() return parser.anchorlist class Crawler(object): count=0 def __init__(self, url): self.q=[url] self.seen=set() parsed=urlparse(url) host=parsed.netloc.split('@')[-1].split(':')[0] self.dom='.'.join(host.split('.')[-2:]) def get_page(self, url, media=False): 'Download page & parse links, add to queue if nec' r=Retriever(url)[0] if fname[0] == '*': print(fname,'...skipping parse') return Crawler.count += 1 print('\n(',Crawler.count,')') print('URL:',url) print('FILE:',fname) self.seen.add(url) ftype=os.path.splitext(fname)[1] if ftype not in ('.htm', '.html'): return for link in r.parse_links(): if link.startswith('mailto:'): print('...discarded, mailto link') continue if not media: ftype=os.path.splitext(link)[1] if ftype in ('.mp3', '.mp4', '.m4v', '.wav'): print('...discarded, media file') continue if not link.startswith('http://'): link=urljoin(url,link) print('*',link) if link not in self.seen: if self.dom not in link: print('...discarded, not in domain') else: if link not in self.q: self.q.append(link) print(', added to Q') else: print('...discarded, already in Q') else: print('...discarded, already processed') def go(self,media=False): 'Process next page in queue (if any)' while self.q: url=self.q.pop() self.get_page(url,media) def main(): if len(sys.argv) > 1: url=sys.argv[1] else: try: url=input('Enter starting URL: ') except (KeyboardInterrupt, EOFError): url='' if not url: return if not url.startswith('http://') and not url.startswith('ftp://'): url='http://%s/' % url robot=Crawler(url) robot.go() if __name__=='__main__': main() ``` 这里报错HTMLParser的__init__()需要一个位置参数而给了它两个 看了下文档,好像这个类只有一个关键字参数,给的例子也是先自定义一个子类再实例化,那么书上这么写是什么意思?要怎么改?求解答


首先声明 下面这几行代码在命令行和eclipse下都能正常运行 import getpass print "nihao1" d = getpass.getpass() print "nihao2" print "d=", d 运行程序只是打印nihao1,然后可以不停的输入,就是不继续往下运行了,程序一直卡在这里,我是windows系统 然后我查看getpass模块的源码,发现getpass.getpass()的调用的是getpass.win_getpass(), 在这个方法中有一行c=msvcrt,getch().程序就是卡在这里不动了。 我能找到的问题就到这了,,接下来也不知道该怎么解决 ,求大神啊!!! PS: 刚才又研究了一下源码,win_getpass()方法源码:里面的print语句是我自己调的时候加上的。 def win_getpass(prompt='Password: ', stream=None): """Prompt for password with echo off, using Windows getch().""" print "hahaha i am in " if sys.stdin is not sys.__stdin__: print "if sys.stdin is not sys.__stdin__:" return fallback_getpass(prompt, stream) import msvcrt print "import msvcrt" for c in prompt: print "for c in prompt:",c msvcrt.putch(c) pw = "" while 1: print "while 1:" c = msvcrt.getch() print "is here " print c if c == '\r' or c == '\n': print "if c == '\r' or c == '\n':" break if c == '\003': print "if c == '\003':" raise KeyboardInterrupt if c == '\b': print "if c == '\b':" pw = pw[:-1] else: print "else:" pw = pw + c msvcrt.putch('\r') msvcrt.putch('\n') print "pw:", pw return pw ``` ``` 第四行有一句 if sys.stdin is not sys.__stdin__: 在windows下 pycharm中,运行源码时这一句返回的是false,所以程序跳过if语句继续往下执行了,这就进入了while 1:循环, 然后就卡在了刚开始说的c=msvcrt.getch()这里了。我把上面的那个if语句改了一下,把is not 改成 is了,竟然可以运行了。只是多了这么几行输出: D:\Python27\lib\ GetPassWarning: Can not control echo on the terminal. return fallback_getpass(prompt, stream) Warning: Password input may be echoed. 我只能做到这个程度了,至于为什么会是这样,为什么那个If语句在起作用,这和pycharm有什么关系,我都不知道了,所以求大神指点啊,说一下所以然,其中的原理是什么,应该怎么弄才能在pycharm中正常运行,因为源码在命令行和eclipse中都是正常的。 谢谢啦!!


![图片说明]( 满屏的坐标,让人奔溃,本应该是这样的 ![图片说明]( ``` import pyautogui, os, time print('Press Control-C to quit.') try: while True: x, y = pyautogui.position() positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4) print(positionStr, end='') #print('\b' * len(positionStr),end='',flush=True) #没有效果。OS系统上清屏命令暂时没有找到 time.sleep(0.3) os.system('clear') except KeyboardInterrupt: print('\nDone') ``` print('\b' * len(positionStr),end='',flush=True),无法清屏 os.system('clear') ,也无法,是Linux上的命令,而OS系统上的命令找不到,有人知道嘛?


我的hadoop集群是在centos上安装的,最近测试安装hue.build完成之后,启动hue爆出如下错误: KeyError: "Couldn't get user id for user hue" 参考: 增加了hue用户,再次启动还是报错. Traceback (most recent call last): File "/home/spark/hue/branch-3.9/build/env/bin/hue", line 11, in <module> load_entry_point('desktop', 'console_scripts', 'hue')() File "/home/spark/hue/branch-3.9/desktop/core/src/desktop/", line 59, in entry execute_from_command_line(sys.argv) File "/home/spark/hue/branch-3.9/build/env/lib/python2.7/site-packages/Django-1.6.10-py2.7.egg/django/core/management/", line 399, in execute_from_command_line utility.execute() File "/home/spark/hue/branch-3.9/build/env/lib/python2.7/site-packages/Django-1.6.10-py2.7.egg/django/core/management/", line 392, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/spark/hue/branch-3.9/build/env/lib/python2.7/site-packages/Django-1.6.10-py2.7.egg/django/core/management/", line 261, in fetch_command commands = get_commands() File "/home/spark/hue/branch-3.9/build/env/lib/python2.7/site-packages/Django-1.6.10-py2.7.egg/django/core/management/", line 107, in get_commands apps = settings.INSTALLED_APPS File "/home/spark/hue/branch-3.9/build/env/lib/python2.7/site-packages/Django-1.6.10-py2.7.egg/django/conf/", line 54, in __getattr__ self._setup(name) File "/home/spark/hue/branch-3.9/build/env/lib/python2.7/site-packages/Django-1.6.10-py2.7.egg/django/conf/", line 49, in _setup self._wrapped = Settings(settings_module) File "/home/spark/hue/branch-3.9/build/env/lib/python2.7/site-packages/Django-1.6.10-py2.7.egg/django/conf/", line 128, in __init__ mod = importlib.import_module(self.SETTINGS_MODULE) File "/home/spark/hue/branch-3.9/build/env/lib/python2.7/site-packages/Django-1.6.10-py2.7.egg/django/utils/", line 40, in import_module __import__(name) File "/home/spark/hue/branch-3.9/desktop/core/src/desktop/", line 34, in <module> from desktop.lib.python_util import force_dict_to_strings File "/home/spark/hue/branch-3.9/desktop/core/src/desktop/lib/", line 23, in <module> from desktop import conf File "/home/spark/hue/branch-3.9/desktop/core/src/desktop/", line 29, in <module> from desktop.lib.conf import Config, ConfigSection, UnspecifiedConfigSection,\ File "/home/spark/hue/branch-3.9/desktop/core/src/desktop/lib/", line 72, in <module> import configobj KeyboardInterrupt 请大家帮我看一下这是什么问题?谢谢


写了一个小的爬虫,准备用APScheduler间隔调用,在scheduler.add_job方法中报错'func must be a callable or a textual reference to one',看了APScheduler的源代码也找不到解决办法,代码如下 def sprider(): scrapy = WebScrapy() scrapy.setUp() scrapy.infoLoad() scrapy.writeExcel() print("The End! %s" % time.strftime("%Y%m%d %H:%M:%S", time.localtime())) if __name__ == "__main__": scheduler = BackgroundScheduler() # scheduler.add_job(run ,'cron', hour='3-4', minute='*', second='*/900') scheduler.add_job(func=sprider(), trigger='interval', id='myrun', minutes=2) try: scheduler.start() print(time.strftime("%Y%m%d %H:%M:%S", time.localtime())) except (KeyboardInterrupt, SystemExit): scheduler.shutdown() print('Errors!') 如果给sprider()随便加一个字符串返回值,则会报错ref-to-obj(func),找不到模块ref这类的错 求大神指教啊


今年,我也32了 ,为了不给大家误导,咨询了猎头、圈内好友,以及年过35岁的几位老程序员……舍了老脸去揭人家伤疤……希望能给大家以帮助,记得帮我点赞哦。 目录: 你以为的人生 一次又一次的伤害 猎头界的真相 如何应对互联网行业的「中年危机」 一、你以为的人生 刚入行时,拿着傲人的工资,想着好好干,以为我们的人生是这样的: 等真到了那一天,你会发现,你的人生很可能是这样的: ...




文章目录Java概述何为编程什么是Javajdk1.5之后的三大版本JVM、JRE和JDK的关系什么是跨平台性?原理是什么Java语言有哪些特点什么是字节码?采用字节码的最大好处是什么什么是Java程序的主类?应用程序和小程序的主类有何不同?Java应用程序与小程序之间有那些差别?Java和C++的区别Oracle JDK 和 OpenJDK 的对比基础语法数据类型Java有哪些数据类型switc...

技术大佬:我去,你写的 switch 语句也太老土了吧

昨天早上通过远程的方式 review 了两名新来同事的代码,大部分代码都写得很漂亮,严谨的同时注释也很到位,这令我非常满意。但当我看到他们当中有一个人写的 switch 语句时,还是忍不住破口大骂:“我擦,小王,你丫写的 switch 语句也太老土了吧!” 来看看小王写的代码吧,看完不要骂我装逼啊。 private static String createPlayer(PlayerTypes p...

和黑客斗争的 6 天!

互联网公司工作,很难避免不和黑客们打交道,我呆过的两家互联网公司,几乎每月每天每分钟都有黑客在公司网站上扫描。有的是寻找 Sql 注入的缺口,有的是寻找线上服务器可能存在的漏洞,大部分都...

Intellij IDEA 实用插件安利

1. 前言从2020 年 JVM 生态报告解读 可以看出Intellij IDEA 目前已经稳坐 Java IDE 头把交椅。而且统计得出付费用户已经超过了八成(国外统计)。IDEA 的...



总结了 150 余个神奇网站,你不来瞅瞅吗?



提到“程序员”,多数人脑海里首先想到的大约是:为人木讷、薪水超高、工作枯燥…… 然而,当离开工作岗位,撕去层层标签,脱下“程序员”这身外套,有的人生动又有趣,马上展现出了完全不同的A/B面人生! 不论是简单的爱好,还是正经的副业,他们都干得同样出色。偶尔,还能和程序员的特质结合,产生奇妙的“化学反应”。 @Charlotte:平日素颜示人,周末美妆博主 大家都以为程序媛也个个不修边幅,但我们也许...






私下里,有不少读者问我:“二哥,如何才能写出一份专业的技术简历呢?我总感觉自己写的简历太烂了,所以投了无数份,都石沉大海了。”说实话,我自己好多年没有写过简历了,但我认识的一个同行,他在阿里,给我说了一些他当年写简历的方法论,我感觉太牛逼了,实在是忍不住,就分享了出来,希望能够帮助到你。 01、简历的本质 作为简历的撰写者,你必须要搞清楚一点,简历的本质是什么,它就是为了来销售你的价值主张的。往深...


据羊城晚报报道,近日中芯国际从荷兰进口的一台大型光刻机,顺利通过深圳出口加工区场站两道闸口进入厂区,中芯国际发表公告称该光刻机并非此前盛传的EUV光刻机,主要用于企业复工复产后的生产线扩容。 我们知道EUV主要用于7nm及以下制程的芯片制造,光刻机作为集成电路制造中最关键的设备,对芯片制作工艺有着决定性的影响,被誉为“超精密制造技术皇冠上的明珠”,根据之前中芯国际的公报,目...


场景 日常开发,if-else语句写的不少吧??当逻辑分支非常多的时候,if-else套了一层又一层,虽然业务功能倒是实现了,但是看起来是真的很不优雅,尤其是对于我这种有强迫症的程序"猿",看到这么多if-else,脑袋瓜子就嗡嗡的,总想着解锁新姿势:干掉过多的if-else!!!本文将介绍三板斧手段: 优先判断条件,条件不满足的,逻辑及时中断返回; 采用策略模式+工厂模式; 结合注解,锦...

离职半年了,老东家又发 offer,回不回?

有小伙伴问松哥这个问题,他在上海某公司,在离职了几个月后,前公司的领导联系到他,希望他能够返聘回去,他很纠结要不要回去? 俗话说好马不吃回头草,但是这个小伙伴既然感到纠结了,我觉得至少说明了两个问题:1.曾经的公司还不错;2.现在的日子也不是很如意。否则应该就不会纠结了。 老实说,松哥之前也有过类似的经历,今天就来和小伙伴们聊聊回头草到底吃不吃。 首先一个基本观点,就是离职了也没必要和老东家弄的苦...


阿里巴巴全球数学竞赛( Alibaba Global Mathematics Competition)由马云发起,由中国科学技术协会、阿里巴巴基金会、阿里巴巴达摩院共同举办。大赛不设报名门槛,全世界爱好数学的人都可参与,不论是否出身数学专业、是否投身数学研究。 2020年阿里巴巴达摩院邀请北京大学、剑桥大学、浙江大学等高校的顶尖数学教师组建了出题组。中科院院士、美国艺术与科学院院士、北京国际数学...


不知道是不是只有我这样子,还是你们也有过类似的经历。 上学的时候总有很多光辉历史,学年名列前茅,或者单科目大佬,但是虽然慢慢地长大了,你开始懈怠了,开始废掉了。。。 什么?你说不知道具体的情况是怎么样的? 我来告诉你: 你常常潜意识里或者心理觉得,自己真正的生活或者奋斗还没有开始。总是幻想着自己还拥有大把时间,还有无限的可能,自己还能逆风翻盘,只不是自己还没开始罢了,自己以后肯定会变得特别厉害...






当HR压你价,说你只值7K时,你可以流畅地回答,记住,是流畅,不能犹豫。 礼貌地说:“7K是吗?了解了。嗯~其实我对贵司的面试官印象很好。只不过,现在我的手头上已经有一份11K的offer。来面试,主要也是自己对贵司挺有兴趣的,所以过来看看……”(未完) 这段话主要是陪HR互诈的同时,从公司兴趣,公司职员印象上,都给予对方正面的肯定,既能提升HR的好感度,又能让谈判气氛融洽,为后面的发挥留足空间。...


HashMap底层实现原理,红黑树,B+树,B树的结构原理 Spring的AOP和IOC是什么?它们常见的使用场景有哪些?Spring事务,事务的属性,传播行为,数据库隔离级别 Spring和SpringMVC,MyBatis以及SpringBoot的注解分别有哪些?SpringMVC的工作原理,SpringBoot框架的优点,MyBatis框架的优点 SpringCould组件有哪些,他们...




爬取彼岸桌面网站较为简单,用到了requests、lxml、Beautiful Soup4


最近微信又搞出了一个大利器,甚至都上了热搜,当然消息最敏捷的自媒体人,纷纷都开通了自己的视频号。01 视频号是什么呢?视频号是微信体系内的短视频,它不同...

面试了一个 31 岁程序员,让我有所触动,30岁以上的程序员该何去何从?



你好,我是Vam的金豆之路,可以叫我豆哥。2019年年度博客之星、技术领域博客专家。主要领域:前端开发。我的微信是 maomin9761,有什么疑问可以加我哦,自己创建了一个微信技术交流群,可以加我邀请你一起交流学习。最后自己也创建了一个微信公众号,里面的文章是我自己精挑细选的文章,主要介绍各种IT新技术。欢迎关注哦,微信搜索:臻美IT,等你来。 欢迎阅读本博文,本博文主要讲述【】,文字通...




已经连续五年参加大厂校招、社招的技术面试工作,简历看的不下于万份 这篇文章会用实例告诉你,什么是差的程序员简历! 疫情快要结束了,各个公司也都开始春招了,作为即将红遍大江南北的新晋UP主,那当然要为小伙伴们做点事(手动狗头)。 就在公众号里公开征简历,义务帮大家看,并一一点评。《启舰:春招在即,义务帮大家看看简历吧》 一石激起千层浪,三天收到两百多封简历。 花光了两个星期的所有空闲时...


前几天我们公司做了一件蠢事,非常非常愚蠢的事情。我原以为从学校出来之后,除了找工作有测试外,不会有任何与考试有关的事儿。 但是,天有不测风云,公司技术总监、人事总监两位大佬突然降临到我们事业线,叫上我老大,给我们组织了一场别开生面的“考试”。 那是一个风和日丽的下午,我翘着二郎腿,左手端着一杯卡布奇诺,右手抓着我的罗技鼠标,滚动着轮轴,穿梭在头条热点之间。 “淡黄的长裙~蓬松的头发...


在2019年的ReactiveConf 上,《Elm in Action》的作者Richard Feldman对未来5年Web开发的发展做了预测,很有意思,分享给大家。如果你有机会从头...