GVKun编程网logo

CherryPy会干扰Windows上的Twistedclosures

17

在这篇文章中,我们将带领您了解CherryPy会干扰Windows上的Twistedclosures的全貌,同时,我们还将为您介绍有关C++Windows:如何closures控制台窗口?、c–64位

在这篇文章中,我们将带领您了解CherryPy会干扰Windows上的Twistedclosures的全貌,同时,我们还将为您介绍有关C ++ Windows:如何closures控制台窗口?、c – 64位Windows上的Boost.Test、CherryPy作为Windows服务 – 示例、closuresWindows服务的显示的知识,以帮助您更好地理解这个主题。

本文目录一览:

CherryPy会干扰Windows上的Twistedclosures

CherryPy会干扰Windows上的Twistedclosures

我有一个应用程序运行Twisted,在启动一些其他线程(包括CherryPy Web服务器reactor.run()在主线程中启动reactor.run()并启动reactor。 这里有一个程序,在Linux上按下Ctrl + C而不是在Windows上时,会干净地closures:

from threading import Thread from signal import signal,SIGINT import cherrypy from twisted.internet import reactor from twisted.web.client import getPage def stop(signum,frame): cherrypy.engine.exit() reactor.callFromThread(reactor.stop) signal(SIGINT,stop) class Root: @cherrypy.expose def index(self): reactor.callFromThread(kickoff) return "Hello World!" cherrypy.server.socket_host = "0.0.0.0" Thread(target=cherrypy.quickstart,args=[Root()]).start() def print_page(html): print(html) def kickoff(): getPage("http://acpstats/account/login").addCallback(print_page) reactor.run()

我相信CherryPy是这里的罪魁祸首,因为这里有一个不用CherryPy编写的程序,当Ctrl + C被按下时,它在Linux和Windows上干净地closures:

from time import sleep from threading import Thread from signal import signal,SIGINT from twisted.internet import reactor from twisted.web.client import getPage keep_going = True def stop(signum,frame): global keep_going keep_going = False reactor.callFromThread(reactor.stop) signal(SIGINT,stop) def print_page(html): print(html) def kickoff(): getPage("http://acpstats/account/login").addCallback(print_page) def periodic_downloader(): while keep_going: reactor.callFromThread(kickoff) sleep(5) Thread(target=periodic_downloader).start() reactor.run()

有谁知道问题是什么? 这是我的难题:

在Linux上一切正常

在Windows上,当CherryPy不运行时,我可以使用reactor.callFromThread从信号处理程序调用函数

当CherryPy运行时,没有任何函数使用reactor.callFromThread从一个信号处理程序中执行(我已经validation信号处理程序本身是否被调用)

我能做些什么呢? 如何在运行CherryPy时从信号处理程序closuresWindows上的Twisted? 这是一个错误,还是我错过了这两个项目的文档的重要部分?

强制python使用旧版本的模块(比我现在安装的)

Python / Twisted程序在Windows下validationSSL证书的正确方法是什么?

python-twisted和SIGKILL

安装扭曲在Python中失败,'没有这样的文件'

Pip不会在Windows上安装Twisted

我怎样才能在python中使用pseudoterminal来模拟一个串行端口?

python执行远程程序

win32api.dll不会安装

连接到(有时是虚拟化的)Windows计算机上运行的服务器时出现的问题

无法在Windows机器上安装扭曲的包

当您调用快速启动时,CherryPy默认处理信号。 在你的情况下,你应该展开快速入门,这只是几行,然后挑选。 这里基本上是快速启动在干线中的作用:

if config: cherrypy.config.update(config) tree.mount(root,script_name,config) if hasattr(engine,"signal_handler"): engine.signal_handler.subscribe() if hasattr(engine,"console_control_handler"): engine.console_control_handler.subscribe() engine.start() engine.block()

在你的情况下,你不需要信号处理程序,所以你可以省略这些。 如果你没有从主线程启动CherryPy,你也不需要调用engine.block。 Engine.block()只是使主线程不会立即终止的一种方法,而是等待进程终止(这是因为autoreload可以可靠地工作;有些平台在除了主线程之外的任何线程中调用execv的问题)。

如果你删除了block()调用,你甚至不需要快速启动的Thread()。 所以,更换你的线路:

Thread(target=cherrypy.quickstart,args=[Root()]).start()

有:

cherrypy.tree.mount(Root()) cherrypy.engine.start()

C ++ Windows:如何closures控制台窗口?

C ++ Windows:如何closures控制台窗口?

我有一个控制台窗口打开使用AllocConsole(),除了主Win32窗口。 控制台窗口在主窗口之前打开。

当我closures主窗口并从主函数返回程序时,控制台保持打开状态(进程也是如此)。 它实际上停留在ntdll的某个地方,因为debugging器显示暂停Visual Studio 2012中的程序。

通过单击Xbuttonclosures它,退出该过程,但使用FreeConsole()closures该过程不会,该过程保持无窗口状态。 CloseWindow(GetConsoleWindow())不closures它,它将它最小化(!?!)。 PostMessage(GetConsoleWindow(),WM_QUIT,0,0)closures窗口,但是进程仍然在工作(从外观上看,这和FreeConsole()是一样的。

有时程序以正确的方式退出,但每次都点击Xbutton。

C ++:一个徘徊的'幽灵'窗口出现..?

Go编译器有一个窗口设置选项吗?

在x86程序中的控制台

C#Windows服务和控制台应用于一体

控制台coutanimation – C ++

我该如何做同样的事情,点击Xbutton呢?

Cout和Cin在Linux中 – 看不到控制台

如何获取我的进程绑定到控制台中的字符数量?

FreePascal控制台video行和列

Windows控制台中的选项卡

控制台在C ++中暂停?

使用PostMessage(wnd,WM_CLOSE,0)来关闭控制台窗口,但问题可能是在程序中的其他地方,即使这可以作为修补程序。 从main()或WinMain()返回时,控制台窗口应自动关闭/消失。

您需要使用FreeConsole销毁WM_DESTROY消息中的控制台。 我通常在一个包装我的控制台的课上做这个。 这样可以将输入/输出重定向到构造函数中的控制台,并在析构函数中重置输入/输出以及分别分配/销毁控制台。

但是,如果不使用类或任何包装器,可以按照以下步骤完成。

例:

#include <windows.h> #include <streambuf> #include <fstream> #include <iostream> std::streambuf *CinBuffer,*CoutBuffer,*CerrBuffer; std::fstream ConsoleInput,ConsoleOutput,ConsoleError; void RedirectIO() { CinBuffer = std::cin.rdbuf(); CoutBuffer = std::cout.rdbuf(); CerrBuffer = std::cerr.rdbuf(); ConsoleInput.open("CONIN$",std::ios::in); ConsoleOutput.open("CONOUT$",std::ios::out); ConsoleError.open("CONOUT$",std::ios::out); std::cin.rdbuf(ConsoleInput.rdbuf()); std::cout.rdbuf(ConsoleOutput.rdbuf()); std::cerr.rdbuf(ConsoleError.rdbuf()); } void ResetIO() { ConsoleInput.close(); ConsoleOutput.close(); ConsoleError.close(); std::cin.rdbuf(CinBuffer); std::cout.rdbuf(CoutBuffer); std::cerr.rdbuf(CerrBuffer); CinBuffer = NULL; CoutBuffer = NULL; CerrBuffer = NULL; } LRESULT __stdcall WindowProcedure(HWND Hwnd,UINT Msg,WParaM wParam,LParaM lParam) { switch(Msg) { case WM_CREATE: AllocConsole(); RedirectIO(); std::cout<<"HELLO CONSOLE!"<<std::endl; break; case WM_DESTROY: std::cout<<"BYE-BYE CONSOLE!"<<std::endl; ResetIO(); FreeConsole(); PostQuitMessage(0); return 0; default: return DefWindowProc(Hwnd,Msg,wParam,lParam); } return 0; }; int main() { WNDCLASSEX WndClass = { sizeof(WNDCLASSEX),CS_DBLCLKS,WindowProcedure,GetmoduleeHandle(NULL),LoadIcon(NULL,IDI_APPLICATION),LoadCursor(NULL,IDC_ARROW),HBrush(COLOR_WINDOW + 1),NULL,"WindowClass",LoadIcon (NULL,IDI_APPLICATION) }; if(RegisterClassEx(&WndClass)) { HWND WindowHandle = CreateWindowEx(0,"Window Title",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,500,NULL); if(WindowHandle) { MSG msg = {NULL}; ShowWindow(WindowHandle,SW_SHOWDEFAULT); while(GetMessage(&msg,0)) { TranslateMessage(&msg); dispatchMessage(&msg); } } } }

#include <windows.h> HWND myConsole = GetConsoleWindow(); //window handle ShowWindow(myConsole,0); //handle window

我使用的解决方案是将Linker-> System-> SubSystem设置为“Windows(/ SUBSYstem:WINDOWS)”而不是“Console(/ SUBSYstem:CONSOLE)”。 这使得控制台甚至不会出现,从而避免了闪烁。 第二个解决方案对我来说比较好,而且第一个已经过时了。

c – 64位Windows上的Boost.Test

c – 64位Windows上的Boost.Test

我正在尝试在Visual Studio 2010中使用Boost.Test,而我在使其工作时遇到了一些麻烦.这是来源:
#include "stdafx.hpp"

#define BOOST_TEST_MODULE (main)
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(morphology)
{
    BOOST_CHECK(true);
}

stdafx.hpp实际上是空的.该项目是作为一个空C项目创建的,设置为构建一个应用程序,目录设置为指向boost头和libs.我正在使用/ entry来指向Boost.Test提供的主要功能.

似乎有一些事情正在发生.首先,在构建时,链接器报告测试库中所有对象的警告,声称“.CRT部分存在;可能存在未处理的静态初始化器或终结器”:

1>msvcprtd.lib(locale0_implib.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>morphology.obj : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(unit_test_main.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(framework.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(test_tools.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(unit_test_log.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(unit_test_suite.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(results_reporter.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(results_collector.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(unit_test_parameters.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(unit_test_monitor.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(debug.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(progress_monitor.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(plain_report_formatter.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(xml_report_formatter.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators

艺术运行时,测试应用程序在Boost.Test库提供的主要调用树下的某个Boost.Test库中崩溃:

>   Pentachoron.Test.exe!std::list<boost::shared_ptr<boost::runtime::cla::parameter>,std::allocator<boost::shared_ptr<boost::runtime::cla::parameter> > >::begin()  Line 787 + 0x13 bytes   C++
    Pentachoron.Test.exe!boost::unit_test::for_each::begin<std::list<boost::shared_ptr<boost::runtime::cla::parameter>,std::allocator<boost::shared_ptr<boost::runtime::cla::parameter> > > >(const std::list<boost::shared_ptr<boost::runtime::cla::parameter>,std::allocator<boost::shared_ptr<boost::runtime::cla::parameter> > > & t,boost::mpl::bool_<1> __formal)  Line 107 + 0xf bytes  C++
    Pentachoron.Test.exe!boost::runtime::cla::parser::operator[](boost::unit_test::basic_cstring<char const > string_id)  Line 169 + 0x41 bytes C++
    Pentachoron.Test.exe!boost::unit_test::runtime_config::`anonymous namespace'::retrieve_parameter<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >(boost::unit_test::basic_cstring<char const > parameter_name,const boost::runtime::cla::parser & s_cla_parser,const std::basic_string<char,std::allocator<char> > & default_value,std::allocator<char> > & optional_value)  Line 216 + 0x34 bytes  C++
    Pentachoron.Test.exe!boost::unit_test::runtime_config::report_sink()  Line 470 + 0x8b bytes C++
    Pentachoron.Test.exe!boost::unit_test::results_reporter::`anonymous namespace'::results_reporter_impl::results_reporter_impl()  Line 59 + 0x31 bytes    C++
    Pentachoron.Test.exe!boost::unit_test::results_reporter::`anonymous namespace'::s_rr_impl()  Line 91 + 0x35 bytes   C++
    Pentachoron.Test.exe!boost::unit_test::results_reporter::get_stream()  Line 120 + 0x5 bytes C++
    Pentachoron.Test.exe!`boost::unit_test::unit_test_main'::`1'::catch$3()  Line 207 + 0x5 bytes   C++
    msvcr100d.dll!_CallSettingFrame()  Line 44  Asm
    msvcr100d.dll!__CxxCallCatchBlock(_EXCEPTION_RECORD * pExcept)  Line 1337 + 0x15 bytes  C++
    ntdll.dll!0000000077c50c21()    
    [Frames below may be incorrect and/or missing,no symbols loaded for ntdll.dll] 
    Pentachoron.Test.exe!boost::unit_test::unit_test_main(boost::unit_test::test_suite * (int,char * *)* init_func,int argc,char * * argv)  Line 179 C++
    Pentachoron.Test.exe!main(int argc,char * * argv)  Line 238    C++
    kernel32.dll!000000007753652d()     
    ntdll.dll!0000000077c2c521()

请….有人告诉我,这里只有一个我没有设置的编译器标志,或者像那样愚蠢……

解决方法

好吧,我明白了.问题是由于我将/ entry设置为直接指向main,它绕过了所有静态对象构造函数和运行时初始化.一旦我设置了/ subsystem:console选项并让编译器正确设置运行时并调用Boost.Test提供的main函数,它就按预期工作了.

多么麻烦.

/ subsystem:console参数用于MSVC链接器.要对其进行配置,请右键单击项目,转到“链接器”|“命令行”,然后将/ subsystem:console添加到对话框底部的“其他选项”. (VS2013)

CherryPy作为Windows服务 – 示例

CherryPy作为Windows服务 – 示例

我见过不less人为此而苦恼 – 我也有问题。 如果有人能够帮助CherryPy服务的例子工作,这将是一个很大的帮助。 解释这些问题将不胜感激。

有一个CherryPy作为一个Windows服务的例子可以在: CherryPy Wiki 。 我正在努力使其工作。 这是我的代码:

""" The most basic (working) CherryPy 3.0 Windows service possible. Requires Mark Hammond's pywin32 package. """ import cherrypy import win32serviceutil import win32service import win32event import servicemanager class HelloWorld: """ Sample request handler class. """ def index(self): return "Hello World!" index.exposed = True class MyService(win32serviceutil.ServiceFramework): """NT Service.""" _svc_name_ = "CherryPyService" _svc_display_name_ = "CherryPy Service" def __init__(self,args): win32serviceutil.ServiceFramework.__init__(self,args) # create an event that SvcDoRun can wait on and SvcStop # can set. self.stop_event = win32event.CreateEvent(None,None) def SvcDoRun(self): self.ReportServiceStatus(win32service.SERVICE_START_PENDING) servicemanager.LogMsg(servicemanager.EVENTLOG_informatION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_,'')) cherrypy.tree.mount(HelloWorld(),'/') # in practice,you will want to specify a value for # log.error_file below or in your config file. If you # use a config file,be sure to use an absolute path to # it,as you can't be assured what path your service # will run in. cherrypy.config.update({ 'global':{ 'engine.autoreload.on': False,'log.screen': False,'log.error_file': 'c:\MG\temp\CherryPy_Sample_Service.log','engine.SIGHUP': None,'engine.SIGTERM': None } }) # set blocking=False so that start() does not block cherrypy.server.quickstart() cherrypy.engine.start(blocking=False) # Now,block until our event is set... win32event.WaitForSingleObject(self.stop_event,win32event.INFINITE) self.ReportServiceStatus(win32service.SERVICE_RUNNING) def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) cherrypy.server.stop() win32event.SetEvent(self.stop_event) if __name__ == '__main__': win32serviceutil.HandleCommandLine(MyService)

以上不同于链接的例子。 我已经添加

self.ReportServiceStatus(win32service.SERVICE_START_PENDING)和

self.ReportServiceStatus(win32service.SERVICE_RUNNING)作为self.ReportServiceStatus(win32service.SERVICE_RUNNING)第一个和最后一个语句

'log.error_file': 'c:\MG\temp\CherryPy_Sample_Service.log',如wiki指示

重要说明 :虽然可以使用控制台python cherrypy_sample_service.py install来安装服务,但是不可能使用python cherrypy_sample_service.py start命令来启动它。 原因是,这种方式创build的服务将引用python可执行文件,这不是作为服务devise的。

JNA Windows服务启动types

使用WIX向参数注册Windows服务

与MEF集成的Autofac

tcp错误代码10060调用WCF服务

在Windows服务中更改事件日志

因此,为了进一步testing,我使用以下代码编译了代码:

from cx_Freeze import setup,Executable exe = Executable( script='cherrypy_sample_service.py' ) build_options = {'includes': ['cherrypy','win32serviceutil','win32service','win32event','servicemanager']} setup( name = "CherryPy Sample Service",version = "0.1",service = ["cherrypy_sample_service.py"],options = {"build_exe" : build_options},executables = [exe])

在构build期间,我收到以下警告:

Python27Applibdistutilsdist.py:267: UserWarning: UnkNown distribution option: 'service' warnings.warn(msg)

我已经根据下面的堆栈问题答案添加了这个选项。

现在我可以调用cherrypy_sample_service install , cherrypy_sample_service remove和cherrypy_sample_service update 。 然而,试图运行服务(无论是从服务或通过cherrypy_sample_service start )导致以下错误:

Error starting service: The service did not respond to the start or control request in a timely fashion.

而我卡住了。 日志文件甚至没有创build。 你能帮我做个例子吗? 如果我们能够解释和运行一个例子,我想这对其他人在处理相关问题时也是有用的。 谢谢!

batch file检查系统服务是否正在运行

Apache的服务PHP的exec不工作

在Windows服务中运行100多个线程是否很好?

从服务的执行线程执行命令行

python服务员作为一个Windows服务

我刚刚从你提到的维基页面上获得了3.1版本,并且马上就能正常工作。 即它从命令行安装,启动和停止,因为它从服务管理GUI启动和停止。

""" The most basic (working) CherryPy 3.1 Windows service possible. Requires Mark Hammond's pywin32 package. """ import cherrypy import win32serviceutil import win32service class HelloWorld: """ Sample request handler class. """ @cherrypy.expose def index(self): return "Hello World!" class MyService(win32serviceutil.ServiceFramework): """NT Service.""" _svc_name_ = "CherryPyService" _svc_display_name_ = "CherryPy Service" def SvcDoRun(self): cherrypy.tree.mount(HelloWorld(),as you can't be assured what path your service # will run in. cherrypy.config.update({ 'global':{ 'log.screen': False,'engine.autoreload.on': False,'engine.SIGTERM': None } }) cherrypy.engine.start() cherrypy.engine.block() def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) cherrypy.engine.exit() self.ReportServiceStatus(win32service.SERVICE_STOPPED) # very important for use with py2exe # otherwise the Service Controller never kNows that it is stopped ! if __name__ == '__main__': win32serviceutil.HandleCommandLine(MyService)

测试环境是:XP SP3,Python 2.7.5,CherryPy 3.3,0,pywin32 218.4。

调试技巧

不需要构建二进制文件来运行您的服务。 当您从源代码安装pywin32服务时,它使用%PYDIR%Libsite-packageswin32pythonservice.exe作为服务代理。 即所有的服务都有它作为运行命令行,有REG:HKEY_LOCAL_MACHInesYstemCurrentControlSetServicesCherryPyServicePythonClass定义服务的实际Python类。

从命令行运行pythonservice.exe ,并确保它运行没有错误( 1 )。 您可能还会注意到,有一个调试选项可以让您将服务的stdout和stderr附加到终端( 2 )。

确保没有阻止pythonservice.exe从网络活动的防火墙软件。

最后,我找到了解决方案 – 我已经下载并重新安装pywin32 …一切工作完全正常!

closuresWindows服务的显示

closuresWindows服务的显示

我已经写了一个Windows服务,应该在某些情况下closures显示器。 我使用的代码是:

private const int MONITOR_OFF = 2; SendMessage(GetDesktopWindow().ToInt32(),WM_SYSCOMMAND,SC_MONITORPOWER,MONITOR_OFF);

这个相同的代码从Windows应用程序工作,但从Windows服务执行时,没有任何反应。 也没有例外, Marshal.GetLastWin32Error()也返回0。

任何想法,我可能会失踪?

Windows服务代码在C,在哪里编写服务逻辑代码?

如何创build一个侦听已更改文件或新文件的文件夹

如何从TFS构build定义输出Windows服务项目

WTSQueryUserToken总是抛出“试图引用一个不存在的令牌”在Windows 7上在C#

存储Windows(NT)服务使用的密码

Windows服务的替代方法

如何终止作为服务运行的应用程序?

Windows任务计划程序将只运行一次应用程序

将焦点集中到使用CreateProcessAsUser从系统服务启动的窗口

系统用户的任务栏图标在哪里?

因为服务没有对​​桌面的默认访问权限,所以你对GetDesktopWindow()的调用返回的结果不是你期望的; 然后盲目地将其转换为Int32并将其用作SendMessage()的窗口句柄目标。 由于收件人可能不是一个有效的窗口,它不会做任何事情。

您可以尝试在服务管理实用程序中编辑服务的属性,以允许桌面交互。

服务运行在他们自己的虚拟桌面空间(他们甚至可以拥有自己的用户配置文件空间)。它是Windows内置安全模型的一部分。 最好的办法是在后台运行一个服务,在用户桌面上运行一个用户模式客户端。 根据用户访问级别的不同,用户可能会终止服务/用户模式客户端。 但是你也可以查询进程,以确保用户应用程序仍在运行。

我们今天的关于CherryPy会干扰Windows上的Twistedclosures的分享已经告一段落,感谢您的关注,如果您想了解更多关于C ++ Windows:如何closures控制台窗口?、c – 64位Windows上的Boost.Test、CherryPy作为Windows服务 – 示例、closuresWindows服务的显示的相关信息,请在本站查询。

本文标签: