对于在Python中尝试直到没有错误感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解python运行怎么无视错误,并且为您提供关于Python/Pyside没有错误消息、python–在列表
对于在Python中尝试直到没有错误感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解python运行怎么无视错误,并且为您提供关于Python / Pyside没有错误消息、python – 在列表理解中尝试/除外、Python中尝试多线程编程的一个简明例子、Python,比较数字列表并不断循环,直到没有重复为止的宝贵知识。
本文目录一览:- 在Python中尝试直到没有错误(python运行怎么无视错误)
- Python / Pyside没有错误消息
- python – 在列表理解中尝试/除外
- Python中尝试多线程编程的一个简明例子
- Python,比较数字列表并不断循环,直到没有重复为止
在Python中尝试直到没有错误(python运行怎么无视错误)
我在Python中有一段代码,似乎在概率上导致错误,因为它正在访问服务器,有时该服务器有500个内部服务器错误。我想一直尝试直到没有收到错误为止。我的解决方案是:
while True:
try:
#code with possible error
except:
continue
else:
#the rest of the code
break
对我来说,这似乎是一种hack。有没有更Python化的方式来做到这一点?
Python / Pyside没有错误消息
通过Macports.
当我想测试这个非常简单的代码时,python意外崩溃:
# -*- coding: utf-8 -*- #!/usr/bin/env python import sys from PySide import QtCore,QtGui app = QtGui.QApplication(sys.argv) app.exec_()
而
# -*- coding: utf-8 -*- #!/usr/bin/env python import sys from PyQt4 import Qt app = Qt.QApplication(sys.argv) app.exec_()
工作良好. (即使它没有显示任何内容,它只是一个裸应用程序)
问题是,python只是崩溃并且没有显示任何错误消息.有谁知道现在该做什么?
以下是Mac OS生成的报告:
Process: Python [1494] Path: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python Identifier: Python Version: ??? (???) Code Type: X86-64 (Native) Parent Process: Python [1450] Date/Time: 2014-01-10 16:44:52.411 +0100 OS Version: Mac OS X 10.7.5 (11G63) Report Version: 9 Interval Since Last Report: 9814 sec Crashes Since Last Report: 29 Per-App Crashes Since Last Report: 28 Anonymous UUID: FA6527AF-EC91-4B5B-B1B3-23AF3ED13089 Crashed Thread: 0 dispatch queue: com.apple.main-thread Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000000 VM Regions Near 0: --> __TEXT 000000010f1ce000-000000010f1d0000 [ 8K] r-x/rwx SM=COW /opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python Application Specific information: objc[1494]: garbage collection is OFF Thread 0 Crashed:: dispatch queue: com.apple.main-thread 0 libsystem_c.dylib 0x00007fff8c5b64f0 strlen + 16 1 libsystem_c.dylib 0x00007fff8c55234b strdup + 18 2 libshiboken-python2.7.1.2.dylib 0x00000001133960dd Shiboken::sequencetoArgcArgv(_object*,int*,char***,char const*) + 427 3 QtGui.so 0x0000000113d37bf7 QApplicationConstructorStart(_object*) + 69 4 QtGui.so 0x0000000113d38e08 Sbk_QApplication_Init + 384 5 org.python.python 0x000000010f2306d4 type_call + 372 6 org.python.python 0x000000010f1df521 PyObject_Call + 97 7 org.python.python 0x000000010f270dda PyEval_EvalFrameEx + 10618 8 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 9 org.python.python 0x000000010f26dc46 PyEval_EvalCode + 54 10 org.python.python 0x000000010f294d9e PyRun_FileExFlags + 174 11 org.python.python 0x000000010f26a2ca builtin_execfile + 490 12 org.python.python 0x000000010f270b17 PyEval_EvalFrameEx + 9911 13 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 14 org.python.python 0x000000010f275a0d fast_function + 285 15 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 16 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 17 org.python.python 0x000000010f2700e6 PyEval_EvalFrameEx + 7302 18 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 19 org.python.python 0x000000010f275a0d fast_function + 285 20 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 21 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 22 org.python.python 0x000000010f275a0d fast_function + 285 23 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 24 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 25 org.python.python 0x000000010f275a0d fast_function + 285 26 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 27 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 28 org.python.python 0x000000010f275a0d fast_function + 285 29 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 30 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 31 org.python.python 0x000000010f275a0d fast_function + 285 32 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 33 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 34 org.python.python 0x000000010f20307b function_call + 347 35 org.python.python 0x000000010f1df521 PyObject_Call + 97 36 org.python.python 0x000000010f272f76 PyEval_EvalFrameEx + 19222 37 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 38 org.python.python 0x000000010f20307b function_call + 347 39 org.python.python 0x000000010f1df521 PyObject_Call + 97 40 org.python.python 0x000000010f272f76 PyEval_EvalFrameEx + 19222 41 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 42 org.python.python 0x000000010f275a0d fast_function + 285 43 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 44 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 45 org.python.python 0x000000010f275a0d fast_function + 285 46 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 47 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 48 org.python.python 0x000000010f20307b function_call + 347 49 org.python.python 0x000000010f1df521 PyObject_Call + 97 50 org.python.python 0x000000010f272f76 PyEval_EvalFrameEx + 19222 51 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 52 org.python.python 0x000000010f275a0d fast_function + 285 53 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 54 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 55 org.python.python 0x000000010f275a0d fast_function + 285 56 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 57 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 58 org.python.python 0x000000010f275a0d fast_function + 285 59 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 60 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 61 org.python.python 0x000000010f275a0d fast_function + 285 62 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 63 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 64 org.python.python 0x000000010f26dc46 PyEval_EvalCode + 54 65 org.python.python 0x000000010f294d9e PyRun_FileExFlags + 174 66 org.python.python 0x000000010f294900 PyRun_SimpleFileExFlags + 768 67 org.python.python 0x000000010f2a87a8 Py_Main + 2952 68 org.python.python 0x000000010f1cff24 0x10f1ce000 + 7972 Thread 1: 0 libsystem_kernel.dylib 0x00007fff8f637d7a __recvfrom + 10 1 _socket.so 0x000000011063e39c sock_recv_guts + 460 2 _socket.so 0x000000011063cfc1 sock_recv + 129 3 org.python.python 0x000000010f272f5f PyEval_EvalFrameEx + 19199 4 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 5 org.python.python 0x000000010f275a0d fast_function + 285 6 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 7 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 8 org.python.python 0x000000010f275a0d fast_function + 285 9 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 10 org.python.python 0x000000010f2759a2 fast_function + 178 11 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 12 org.python.python 0x000000010f2759a2 fast_function + 178 13 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 14 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 15 org.python.python 0x000000010f20307b function_call + 347 16 org.python.python 0x000000010f1df521 PyObject_Call + 97 17 org.python.python 0x000000010f1eb196 instancemethod_call + 502 18 org.python.python 0x000000010f1df521 PyObject_Call + 97 19 org.python.python 0x000000010f2751b8 PyEval_CallObjectWithKeywords + 168 20 org.python.python 0x000000010f2aab46 t_bootstrap + 70 21 libsystem_c.dylib 0x00007fff8c5628bf _pthread_start + 335 22 libsystem_c.dylib 0x00007fff8c565b75 thread_start + 13 Thread 2: 0 libsystem_kernel.dylib 0x00007fff8f6387e6 kevent + 10 1 libzmq.3.dylib 0x0000000110f075c9 zmq::kqueue_t::loop() + 165 2 libzmq.3.dylib 0x0000000110f198ce thread_routine + 106 3 libsystem_c.dylib 0x00007fff8c5628bf _pthread_start + 335 4 libsystem_c.dylib 0x00007fff8c565b75 thread_start + 13 Thread 3: 0 libsystem_kernel.dylib 0x00007fff8f6387e6 kevent + 10 1 libzmq.3.dylib 0x0000000110f075c9 zmq::kqueue_t::loop() + 165 2 libzmq.3.dylib 0x0000000110f198ce thread_routine + 106 3 libsystem_c.dylib 0x00007fff8c5628bf _pthread_start + 335 4 libsystem_c.dylib 0x00007fff8c565b75 thread_start + 13 Thread 4: 0 libsystem_kernel.dylib 0x00007fff8f638a7a poll + 10 1 libzmq.3.dylib 0x0000000110f1fa47 zmq_poll + 460 2 libzmq.3.dylib 0x0000000110f0eb4c zmq::proxy(zmq::socket_base_t*,zmq::socket_base_t*,zmq::socket_base_t*) + 138 3 _device.so 0x0000000110fb02be __pyx_pw_3zmq_7backend_6cython_7_device_3proxy + 926 4 org.python.python 0x000000010f1df521 PyObject_Call + 97 5 _device.so 0x0000000110fb144a __pyx_pw_3zmq_7backend_6cython_7_device_1device + 1130 6 org.python.python 0x000000010f270b17 PyEval_EvalFrameEx + 9911 7 org.python.python 0x000000010f2759a2 fast_function + 178 8 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 9 org.python.python 0x000000010f2759a2 fast_function + 178 10 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 11 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 12 org.python.python 0x000000010f20307b function_call + 347 13 org.python.python 0x000000010f1df521 PyObject_Call + 97 14 org.python.python 0x000000010f1eb196 instancemethod_call + 502 15 org.python.python 0x000000010f1df521 PyObject_Call + 97 16 org.python.python 0x000000010f2751b8 PyEval_CallObjectWithKeywords + 168 17 org.python.python 0x000000010f2aab46 t_bootstrap + 70 18 libsystem_c.dylib 0x00007fff8c5628bf _pthread_start + 335 19 libsystem_c.dylib 0x00007fff8c565b75 thread_start + 13 Thread 5: 0 libsystem_kernel.dylib 0x00007fff8f6387e6 kevent + 10 1 libzmq.3.dylib 0x0000000110f075c9 zmq::kqueue_t::loop() + 165 2 libzmq.3.dylib 0x0000000110f198ce thread_routine + 106 3 libsystem_c.dylib 0x00007fff8c5628bf _pthread_start + 335 4 libsystem_c.dylib 0x00007fff8c565b75 thread_start + 13 Thread 6: 0 libsystem_kernel.dylib 0x00007fff8f6387e6 kevent + 10 1 libzmq.3.dylib 0x0000000110f075c9 zmq::kqueue_t::loop() + 165 2 libzmq.3.dylib 0x0000000110f198ce thread_routine + 106 3 libsystem_c.dylib 0x00007fff8c5628bf _pthread_start + 335 4 libsystem_c.dylib 0x00007fff8c565b75 thread_start + 13 Thread 7: 0 libsystem_kernel.dylib 0x00007fff8f637bca __psynch_cvwait + 10 1 libsystem_c.dylib 0x00007fff8c566274 _pthread_cond_wait + 840 2 org.python.python 0x000000010f2a6541 PyThread_acquire_lock + 97 3 org.python.python 0x000000010f26d97d PyEval_RestoreThread + 61 4 _sqlite3.so 0x0000000110cebd39 pysqlite_step + 41 5 _sqlite3.so 0x0000000110ce6fa8 pysqlite_connection_commit + 232 6 org.python.python 0x000000010f1df521 PyObject_Call + 97 7 org.python.python 0x000000010f1df6ff call_function_tail + 95 8 org.python.python 0x000000010f1df96e PyObject_CallMethod + 318 9 _sqlite3.so 0x0000000110ce862c pysqlite_connection_exit + 124 10 org.python.python 0x000000010f1df521 PyObject_Call + 97 11 org.python.python 0x000000010f1da37e PyObject_CallFunctionObjArgs + 382 12 org.python.python 0x000000010f27269e PyEval_EvalFrameEx + 16958 13 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 14 org.python.python 0x000000010f275a0d fast_function + 285 15 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 16 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 17 org.python.python 0x000000010f20307b function_call + 347 18 org.python.python 0x000000010f1df521 PyObject_Call + 97 19 org.python.python 0x000000010f272f76 PyEval_EvalFrameEx + 19222 20 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 21 org.python.python 0x000000010f275a0d fast_function + 285 22 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 23 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 24 org.python.python 0x000000010f275a0d fast_function + 285 25 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 26 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 27 org.python.python 0x000000010f20307b function_call + 347 28 org.python.python 0x000000010f1df521 PyObject_Call + 97 29 org.python.python 0x000000010f272f76 PyEval_EvalFrameEx + 19222 30 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 31 org.python.python 0x000000010f275a0d fast_function + 285 32 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 33 org.python.python 0x000000010f2759a2 fast_function + 178 34 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 35 org.python.python 0x000000010f2759a2 fast_function + 178 36 org.python.python 0x000000010f270bb8 PyEval_EvalFrameEx + 10072 37 org.python.python 0x000000010f26e416 PyEval_EvalCodeEx + 1990 38 org.python.python 0x000000010f20307b function_call + 347 39 org.python.python 0x000000010f1df521 PyObject_Call + 97 40 org.python.python 0x000000010f1eb196 instancemethod_call + 502 41 org.python.python 0x000000010f1df521 PyObject_Call + 97 42 org.python.python 0x000000010f2751b8 PyEval_CallObjectWithKeywords + 168 43 org.python.python 0x000000010f2aab46 t_bootstrap + 70 44 libsystem_c.dylib 0x00007fff8c5628bf _pthread_start + 335 45 libsystem_c.dylib 0x00007fff8c565b75 thread_start + 13 Thread 8:: dispatch queue: com.apple.libdispatch-manager 0 libsystem_kernel.dylib 0x00007fff8f6387e6 kevent + 10 1 libdispatch.dylib 0x00007fff9172e786 _dispatch_mgr_invoke + 923 2 libdispatch.dylib 0x00007fff9172d316 _dispatch_mgr_thread + 54
解决方法
python script.py
看起来崩溃是由解析sys.argv引起的.检查导致崩溃的系统日志.尝试系统上的其他QT应用程序,看看是否存在错误.
python – 在列表理解中尝试/除外
cleaned_list = [] for item in dirtry_list: try: item.video except Video.DoesNotExist: pass else: cleaned_list.append(item)
解决方法
cleaned_list = [item for item in dirty_list if item.video_id is not None]
Python中尝试多线程编程的一个简明例子
综述
多线程是程序设计中的一个重要方面,尤其是在服务器Deamon程序方面。无论何种系统,线程调度的开销都比传统的进程要快得多。
Python可以方便地支持多线程。可以快速创建线程、互斥锁、信号量等等元素,支持线程读写同步互斥。美中不足的是,Python的运行在Python 虚拟机上,创建的多线程可能是虚拟的线程,需要由Python虚拟机来轮询调度,这大大降低了Python多线程的可用性。希望高版本的Python可以 解决这个问题,发挥多CPU的最大效率。
网上有些朋友说要获得真正多CPU的好处,有两种方法:
1.可以创建多个进程而不是线程,进程数和cpu一样多。
2.使用Jython 或 IronPython,可以得到真正的多线程。
闲话少说,下面看看Python如何建立线程
Python线程创建
使用threading模块的 Thread类
类接口如下
需要关注的参数是target和args. target 是需要子线程运行的目标函数,args是函数的参数,以tuple的形式传递。
以下代码创建一个指向函数worker 的子线程
...
th = threading.Thread(target=worker,args=(i,acc) ) ;
启动这个线程
等待线程返回
或者th.join()
如果你可以对要处理的数据进行很好的划分,而且线程之间无须通信,那么你可以使用:创建=》运行=》回收的方式编写你的多线程程序。但是如果线程之间需要访问共同的对象,则需要引入互斥锁或者信号量对资源进行互斥访问。
下面讲讲如何创建互斥锁
创建锁
....
使用锁
#锁定,从下一句代码到释放前互斥访问
g_mutex.acquire()
a_account.deposite(1)
#释放
g_mutex.release()
最后,模拟一个公交地铁IC卡缴车费的多线程程序
有10个读卡器,每个读卡器收费器每次扣除用户一块钱进入总账中,每读卡器每天一共被刷10000000次。账户原有100块。所以最后的总账应该为10000100。先不使用互斥锁来进行锁定(注释掉了锁定代码),看看后果如何。
import time,datetime import threading def worker(a_tid,a_account): global g_mutex print("Str " , a_tid, datetime.datetime.now() ) for i in range(1000000): #g_mutex.acquire() a_account.deposite(1) #g_mutex.release() print("End " , a_tid , datetime.datetime.now() ) class Account: def __init__ (self, a_base ): self.m_amount=a_base def deposite(self,a_amount): self.m_amount+=a_amount def withdraw(self,a_amount): self.m_amount-=a_amount if __name__ == "__main__": global g_mutex count = 0 dstart = datetime.datetime.now() print("Main Thread Start At: ", dstart) #init thread_pool thread_pool = [] #init mutex g_mutex = threading.Lock() # init thread items acc = Account(100) for i in range(10): th = threading.Thread(target=worker,args=(i,acc) ) ; thread_pool.append(th) # start threads one by one for i in range(10): thread_pool[i].start() #collect all threads for i in range(10): threading.Thread.join(thread_pool[i]) dend = datetime.datetime.now() print("count=", acc.m_amount) print("Main Thread End at: ", dend, " time span ", dend-dstart)
注意,先不用互斥锁进行临界段访问控制,运行结果如下:
从结果看到,程序确实是多线程运行的。但是由于没有对对象account进行互斥访问,所以结果是错误的,只有3434612,比原预计少了很多。
打开锁后:
这次可以看到,结果正确了。运行时间比不进行互斥多了很多,不过这也是同步的代价。
同时发现,写多线程,多进程类的程序,不能用自带的idle来运行。会有错误。