我创建了一个示例,说明如何在 Windows 上使用套接字而不是共享内存信号量来实现此行为。这达到了同样的目的;只要至少有一个 Python 脚本在运行,c++ 程序就会一直运行。一旦所有的脚本都完成了,只要在一定的超时时间内没有更多的 Python 脚本被启动,c++ 程序就会结束。
这里的大部分代码都进入了 c++ 程序,该程序运行一个线程来监视来自 Python 脚本的 TCP 连接。
Python 脚本只是检查/启动 Windows 程序,然后打开一个套接字,该套接字一直保持打开状态,直到脚本结束。
Windows 程序检测套接字连接和断开连接,从而跟踪 Python 脚本的运行时间。
在这些示例中,Windows 程序恰好被称为“ConsoleApplication11.exe”。我使用了 1234 端口和 15 秒的超时,您可以在 c++ 程序的第 19-21 行更改这些。另外,如果您想让 c++ 程序的终止更迅速,请在 client_monitor_thread() 结束时调用 exit() 而不是 return。
希望这可能有用。
Python 脚本:
import socket
import time
import psutil
import os
# check if the C++ program is running, start it if not
cppProgramName = "ConsoleApplication11.exe"
progRunning = False
for pid in psutil.pids():
p = psutil.Process(pid)
if (cppProgramName in p.name()):
progRunning = True
if not progRunning:
os.startfile(cppProgramName)
time.sleep(5) # wait for c++ program to start
# open a socket to the C++ program to tell it we need it to keep running
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 1234))
# (MAIN PROGRAM)
time.sleep(3)
# (END OF MAIN PROGRAM)
# close the socket to the C++ program
s.close()
C++ 程序:
// ConsoleApplication11.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <set>
#include <chrono>
#include <thread>
#include <winsock2.h>
#include <Ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib") // link with Ws2_32.lib
namespace
{
const unsigned int commonPort = 1234; // must match Python app
const unsigned int noClientsTimeoutLimit = 15; // quit when no clients connected for this many seconds
bool clientMonitorRunning = true; // flag to show client monitor is running
void client_monitor_thread()
{
// start up winsock service version 2.2
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR)
{
std::cout << "WSAStartup() failed with error: " << iResult << std::endl;
clientMonitorRunning = false;
return;
}
// create a socket for listening for incoming connection requests.
SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenSocket == INVALID_SOCKET)
{
std::cout << "socket() function failed with error: " << WSAGetLastError() << std::endl;
closesocket(listenSocket);
clientMonitorRunning = false;
return;
}
// sockaddr_in structure specifies the address family, IP address, and port for the socket
sockaddr_in service;
service.sin_family = AF_INET;
inet_pton(AF_INET, (PCSTR)"127.0.0.1", &(service.sin_addr));
service.sin_port = htons(commonPort);
if (SOCKET_ERROR == bind(listenSocket, (SOCKADDR *)& service, sizeof(service)))
{
std::cout << "bind function failed with error " << WSAGetLastError() << std::endl;
closesocket(listenSocket);
clientMonitorRunning = false;
return;
}
// Listen for incoming connection requests on the created socket
if (SOCKET_ERROR == listen(listenSocket, SOMAXCONN))
{
wprintf(L"listen function failed with error: %d\n", WSAGetLastError());
closesocket(listenSocket);
clientMonitorRunning = false;
return;
}
std::cout << "Listening on port " << commonPort << std::endl;
// mow monitor client connections
std::set<unsigned int> activefds;
int timeoutCounter = 0;
while (clientMonitorRunning)
{
// check for existing clients disconnected
if (0 != activefds.size())
{
std::set<unsigned int> disconnectedfds;
for (auto fd : activefds)
{
int flags = 0;
char buf[10];
int rv = recv(fd, buf, 10, flags);
if (0 == rv)
{
disconnectedfds.insert(fd);
}
}
for (auto fd : disconnectedfds)
{
activefds.erase(fd);
}
}
// are any clients connected? do we need to quit?
if (0 == activefds.size())
{
std::cout << "No clients - will exit in " << noClientsTimeoutLimit - timeoutCounter << " seconds" << std::endl;
++timeoutCounter;
if (timeoutCounter == noClientsTimeoutLimit)
{
for (auto fd : activefds)
{
closesocket(fd);
}
closesocket(listenSocket);
clientMonitorRunning = false;
return;
}
}
else
{
timeoutCounter = 0;
}
// check for activity on the listening socket
fd_set readfds;
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(listenSocket, &readfds);
switch (select(sizeof(readfds), &readfds, NULL, NULL, &timeout))
{
case 0: // timeout
{
break;
}
case SOCKET_ERROR:
{
std::cout << "listen failed with error: " << WSAGetLastError() << std::endl;
closesocket(listenSocket);
clientMonitorRunning = false;
return;
}
default:
{
if (FD_ISSET(listenSocket, &readfds))
{
// accept the connection.
SOCKET fd = accept(listenSocket, NULL, NULL);
if (fd == INVALID_SOCKET)
{
std::cout << "accept failed with error: " << WSAGetLastError() << std::endl;
closesocket(listenSocket);
clientMonitorRunning = false;
return;
}
else
{
unsigned long nonBlock = 1;
ioctlsocket(fd, FIONBIO, &nonBlock);
activefds.insert(fd);
}
}
break;
}
}
}
return;
}
}
int main()
{
// start the client monitor thread, which will run until no clients are connected
std::thread clientMonitor(client_monitor_thread);
// placeholder for doing the actual work in this program
// loop until the client monitor thread exits
while (clientMonitorRunning)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
// clean up
clientMonitor.join();
WSACleanup();
return 0;
}