有没有办法Ctrl+C根据嵌入在Cython扩展中的循环中断()Python脚本?
我有以下python脚本:
def main():
# Intantiate simulator
sim = PySimulator()
sim.Run()
if __name__ == "__main__":
# Try to deal with Ctrl+C to abort the running simulation in terminal
# (Doesn't work...)
try:
sys.exit(main())
except (KeyboardInterrupt, SystemExit):
print '\n! Received keyboard interrupt, quitting threads.\n'
Run Code Online (Sandbox Code Playgroud)
这运行一个循环,它是C++ Cython扩展的一部分.然后,在按下时Ctrl+C,KeyboardInterrupt抛出但忽略,程序继续运行直到模拟结束.
我找到的工作是通过捕获SIGINT信号来处理扩展内的异常:
#include <execinfo.h>
#include <signal.h>
static void handler(int sig)
{
// Catch exceptions
switch(sig)
{
case SIGABRT:
fputs("Caught SIGABRT: usually caused by an abort() or assert()\n", stderr); …Run Code Online (Sandbox Code Playgroud) 我正在windows下移植一个Linux/gcc程序,并为两者实现了常见的异常处理.我想知道SIGINTMinGW/gcc 的等效信号是什么.
以下是我在Linux下处理它的方法:
static void handler(int sig)
{
// Catch exceptions
switch(sig)
{
case SIGABRT:
fputs("Caught SIGABRT: usually caused by an abort() or assert()\n", stderr);
break;
case SIGFPE:
fputs("Caught SIGFPE: arithmetic exception, such as divide by zero\n",
stderr);
break;
case SIGILL:
fputs("Caught SIGILL: illegal instruction\n", stderr);
break;
case SIGINT:
fputs("Caught SIGINT: interactive attention signal, probably a ctrl+c\n",
stderr);
break;
case SIGSEGV:
fputs("Caught SIGSEGV: segfault\n", stderr);
break;
case SIGTERM:
default:
fputs("Caught SIGTERM: a termination request was sent to the …Run Code Online (Sandbox Code Playgroud) 我正在写一个Cthon函数的Cython包装器.我有一个带有以下签名的pxd文件:
double contr_hrr(int lena, double xa, double ya, double za, double *anorms)
Run Code Online (Sandbox Code Playgroud)
当我尝试从pyx文件中调用它时
...
return contr_hrr(len(acoefs),a.origin[0],a.origin[1],a.origin[2],anorms2)
Run Code Online (Sandbox Code Playgroud)
其中anorms2是一个python列表,我收到错误消息:
cython/ctwo.pyx:35:80: Cannot convert Python object to 'double *'
Run Code Online (Sandbox Code Playgroud)
如何将python列表作为双数组传递给C函数?
我有一个非常复杂的课程:
class C:
pass
Run Code Online (Sandbox Code Playgroud)
我有这个测试代码:
for j in range(10):
c = C()
print c
Run Code Online (Sandbox Code Playgroud)
这使 :
<__main__.C instance at 0x7f7336a6cb00>
<__main__.C instance at 0x7f7336a6cab8>
<__main__.C instance at 0x7f7336a6cb00>
<__main__.C instance at 0x7f7336a6cab8>
<__main__.C instance at 0x7f7336a6cb00>
<__main__.C instance at 0x7f7336a6cab8>
<__main__.C instance at 0x7f7336a6cb00>
<__main__.C instance at 0x7f7336a6cab8>
<__main__.C instance at 0x7f7336a6cb00>
<__main__.C instance at 0x7f7336a6cab8>
Run Code Online (Sandbox Code Playgroud)
人们可以很容易地看到Python切换到两个不同的值.在某些情况下,这可能是灾难性的(例如,如果我们将对象存储在其他复杂对象中).
现在,如果我将对象存储在List中:
lst = []
for j in range(10):
c = C()
lst.append(c)
print c
Run Code Online (Sandbox Code Playgroud)
我明白了:
<__main__.C instance at 0x7fd8f8f7eb00>
<__main__.C instance at 0x7fd8f8f7eab8> …Run Code Online (Sandbox Code Playgroud) python for-loop memory-management reference-counting instantiation
有没有办法使用Sphinx显示"类"列表/选项卡,或组织生成的html页面按类显示成员,类在视觉上分开?
我使用Sphinx 1.1.3,尝试记录Python扩展(使用Cython创建的自定义扩展).我的问题是如果我进入模块选项卡(这是非常不可读的),整个扩展显示在一个单独的块中,另一方面,"索引"选项卡将所有内容合并在一起(这是正常的).我想要一个每类显示(更接近Doxygen会做什么).
有类似的事情:
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
* :ref:`classindex` ???
Run Code Online (Sandbox Code Playgroud)
存在?
非常感谢.
我需要以eval多线程方式执行R s,这是Rserve非常好的.但是,如果一个实例的eval需要太长时间,我需要能够关闭计算阻塞eval的实例.就我测试而言,给定的实例将拒绝关闭直到eval完成(显然,它需要在再次侦听之前获取结果).所以这是我的问题:
有没有办法在阻塞实例(类似Process对象)上获取java句柄,这样我就可以强行杀死/终止eval(类似的东西process.destroy())?换句话说,当我要求eval(创建连接,抛出命令)时,如何通过java在正在处理的eval和与之相关的Rsere实例之间建立关系?
或者我是否错过了一些已经允许处理这种需求的Rserve?
注意:我已经尝试运行所有(所有evals)serverEval()而不是常规eval,它运行主实例上的计算,但这当然不满意,因为它只使用一个进程(主要进程).那个我可以杀死的,但我的主要目标是能够单独关闭一个阻塞的eval,在一个单独的实例上运行.而且,自然地,保持我的8个CPU内核的优势,也就是说,保持并行性.除此之外没有必要使用Rserve(在这种情况下JRI引擎就足够了).
注意:我想避免这种事情(线程),在不同的端口上处理主服务器本身的几个实例.这不是一种选择.
我已经尝试获取有关Rserve邮件列表的信息,但尚未得到答复.我希望自己能够清楚地在这里得到答案或有用的评论.如果没有,请询问详情.非常感谢提前.
编辑:我还测试了RCaller,它处理R需要的任意数量的实例,但是,因为它将结果写入XML文件以便稍后从java端解析(不像Rserve那样真正使用通信协议),它也是我必须执行的速度慢...
该程序应该采用图像的轮廓,然后将其分成不同的象限,然后对其进行着色,例如Andy Warhol Marilyn Monroe图片.
直到"Warholize"功能的每个功能都有效,但是在我不知道该怎么做c=getPixel(picEdge,x,y)的warholize功能下它会被卡住.任何帮助将不胜感激.它应该做"让c为位于x,y的picEdge中像素的颜色"
def main():
pic= makePicture( pickAFile() )
show( pic )
threshold= 10
edgePic= makeOutline( pic, threshold )
warholize(pic)
show(warholize(pic))
def difference( a, b ):
if a > b :
return a - b
else:
return b - a
def intensity( px ) :
r= getRed( px )
g= getBlue( px )
b= getGreen( px )
avg= ( r + g + b ) / 3
return avg
def makeOutline( pic, threshold ): …Run Code Online (Sandbox Code Playgroud) “顺序编程语言”概念背后到底是什么?
我找不到合适/深入的描述。我清楚地看到什么是顺序算法,而是一种编程语言。
Sequential可能会反对procedural,但是...
什么是“严格顺序”的语言?这只是提供或不提供处理功能的可能性的问题吗?
我知道这个问题有点太笼统了,需要的不仅仅是简单的问答,但是关于该主题的介绍和/或一些好的指示将不胜感激。
编辑:
嗯,经过进一步阅读(感谢msw和Edorka),我意识到上面引用的假设是我对一些基本词汇的误解的结果,并且不依赖于任何具体的内容。因此这个问题变得毫无意义。但感谢你们的时间和宝贵的进展!
编辑 :sequential最好反对...
请耐心等待,我几周前才开始使用 python。
我正在使用 JES。
我做了一个将图片转换为灰度的函数。我为每种颜色 r 和 r1、g 和 g1、b 和 b1 创建了两个名称。这背后的想法是将原始值保留在内存中,以便图片可以恢复为其原始颜色。
def grayScale(pic):
for p in getPixels(pic):
r = int(getRed(p))
g = int(getGreen(p))
b = int(getBlue(p))//I have tried this with and without the int()
r1=r
g1=g
b1=b
new = (r + g + b)/3
color= makeColor(new,new,new)
setColor(p, color)
def restoreColor(pic):
for p in getPixels(pic):
setColor (p, makeColor(r1,g1,b1))
Run Code Online (Sandbox Code Playgroud)
它不起作用。 The error: "local or global name could not be found."
我明白为什么我会收到这个错误。
但是,如果我尝试在 restoreColor 中定义它们,它将给出灰度值。
我明白为什么我会收到这个错误,但不知道如何格式化我的代码,以保存名称值。我查看了有关局部和全局变量/名称的问题;但我无法在我学到的基本语法中弄清楚如何做到这一点。
问题是:
如何创建名称并获取原始(红色、绿色、蓝色)的值,然后我可以在另一个函数中使用它们?我尝试过的一切都返回了更改后的(灰度)值。谢谢
我正在包装一些C库,我有一个功能,在某些情况下可能导致分段错误.在这种情况下,我需要调用第二个函数,在那种情况下将成功完成.
有谁知道如何处理cython中的分段错误?
我有以下 C++ 类:
。H
class ALabSet: public LabSet {
public:
PyObject *m_obj;
ALabSet(PyObject *obj);
virtual ~ALabSet();
PyObject *GetPyObj();
};
Run Code Online (Sandbox Code Playgroud)
.CPP
ALabSet::ALabSet(PyObject *obj): LabSet() {
this->m_obj = obj;
// Provided by "cyelp_api.h"
if (import_cyelp()) {
} else {
Py_XINCREF(this->m_obj);
}
}
ALabSet::~ALabSet() {
Py_XDECREF(this->m_obj);
}
PyObject *ALabSet::GetPyObj() {
return this->m_obj;
}
Run Code Online (Sandbox Code Playgroud)
我用 Cython 将其暴露如下:
cdef extern from "adapter/ALabSiteSetsManager.h" namespace "elps" :
cdef cppclass ALabSet:
ALabSet(PyObject *)
PyObject *GetPyObj()
cdef class PyLabSet:
cdef ALabSet *thisptr
def __cinit__(self):
self.thisptr = new ALabSet(<PyObject *>self) …Run Code Online (Sandbox Code Playgroud) 我想扩大图片的一部分,在这个例子中,是一个鼻子.
我有一个功能来选择我要放大的图片部分.
def copyAndPaste(picture):
height = getHeight(picture)
width = getWidth(picture)
newPicture = makeEmptyPicture(width, height)
for x in range(width):
for y in range(height):
pxl = getPixel(picture,x,y)
if (x>48 and x<59) and (y>58 and y<71):
newPxl =getPixel(newPicture, #?,#?)
else:
newPxl = getPixel(newPicture, x,y)
color = getColor(pxl)
setColor(newPxl,color)
return newPicture
def d():
f=pickAFile()
picture=makePicture(f)
newPicture = copyAndPaste(picture)
writePictureTo(newPicture, r"D:\FOLDER\0Pic4.jpg")
explore (newPicture)
Run Code Online (Sandbox Code Playgroud)

我还有一个放大图片的功能:
def Enlarge(picture):
height = getHeight(picture)
width = getWidth(picture)
newPicture = makeEmptyPicture(width*2, height*2)
x1=0
for x in range(0,width):
y1=0
for y in …Run Code Online (Sandbox Code Playgroud)