简体   繁体   中英

Running multiple instances of the same XPC service (NSXPCConnection)

Is it possible to run multiple instances of the same XPC service using the XPC APIs found in Foundation.framework ( NSXPCConnection , etc.)? The docs don't provide much insight on this matter.

EDIT : Did a quick test, and it seems like only one instance of the service is running even though I created two XPC connections. Is there any way to have it run another instance?

A bit late, but the definitive answer to this question is provided in the xpcservice.plist manpage :

ServiceType (default: Application)

The type of the XPC Service specifies how the service is instantiated. The values are:

• Application: Each application will have a unique instance of this service.

• User: There is one instance of the service process created for each user.

• System: There is one instance of the service process for the whole system. System XPC Services are restricted to reside in system frameworks and must be owned by root.

Bottom line: In most cases there is a single instance of an XPC Service and only in the case where different applications can connect to the same service (not even possible when the service is bundled with an app), will there be multiple instances (one-instance-per-app).

I believe XPC services designed for one instance per multiple connections. Probably, it is more convenient to manage named pipes with one running executable. So, the most likely it is impossible to create multiple instances simultaneously.

Since XPC services should have no state, it should not matter, whether one ore more instances are running:

XPC services are managed by launchd, which launches them on demand, restarts them if they crash, and terminates them (by sending SIGKILL ) when they are idle. This is transparent to the application using the service, except for the case of a service that crashes while processing a message that requires a response. In that case, the application can see that its XPC connection has become invalid until the service is restarted by launchd. Because an XPC service can be terminated suddenly at any time, it must be designed to hold on to minimal state—ideally, your service should be completely stateless, although this is not always possible.

–– Creating XPC Services

Put all neccessary state information into the xpc call and deliver it back to the client, if it has to persist.

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/xpcservice.plist.5.html

ServiceType key in XPCService dictionary: Application or User or System

But this 'ServiceType' is irrelevant… IF Service is embedded in an application bundle then it will only be visible to the containing application and will be, by definition, Applicaton-type services. A subsequent connection request from an application to a service will result in a new connection to an existing service.

我知道我迟到了,虽然你不能用普通的XPC做到这一点,但是库(OpenEmu的一个组件)应该可以做你所要求的: OpenEmuXPCCommunicator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM