Single Instance Application with command line interface
15680 단어 application
Single Instance Application with command line interface
Posted on February 17th, 2009
No comments
I wanted a python gtk application to open a new window on its first execution and then have subsequent executions send their command line arguments to the initial application rather than starting a new one. Here is the template which provides that functionality:
download single instance app. friends
"""
This will only spawn one gtk application at a time. If this command is executed
while an instance is already running, the command line arguments are sent to the
already running application.
"""
import sys
import pygtk
pygtk.require('2.0')
import gtk
import socket
import threading
import SocketServer
class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
data = self.request.recv(1024)
cur_thread = threading.currentThread()
# do something with the request:
self.server.app.label.set_label(data)
# could instead of the length of the input, could return error codes, more
# information (if the request was a query), etc. Using a length function
# as a simple example
response = 'string length: %d' % len(data)
print 'responding to',data,'with',response
self.request.send(response)
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
stopped = False
allow_reuse_address = True
def serve_forever(self):
while not self.stopped:
self.handle_request()
def force_stop(self):
self.server_close()
self.stopped = True
self.create_dummy_request()
def create_dummy_request(self):
client(self.server_address[0], self.server_address[1], 'last message for you')
def client(ip, port, message):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
sock.send(message)
response = sock.recv(1024)
print "Received: %s" % response
sock.close()
def start_server(host, port):
server = ThreadedTCPServer((host, port), ThreadedTCPRequestHandler)
ip, port = server.server_address
# Start a thread with the server -- that thread will then start one
# more thread for each request
server_thread = threading.Thread(target=server.serve_forever)
# Exit the server thread when the main thread terminates
server_thread.setDaemon(True)
server_thread.start()
return server
class SingleInstanceApp:
def destroy(self, widget, data=None):
self.server.force_stop()
gtk.main_quit()
#exit(1) # I'm sorry but mozembed is making a huge pain in my ass
def __init__(self, server):
self.server = server
# create a new window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_default_size(300,30)
self.window.connect("destroy", self.destroy)
self.label = gtk.Label("hello world")
self.window.add(self.label)
self.window.show_all()
# and the window
self.window.show()
def main(self):
gtk.gdk.threads_init()
gtk.main()
if __name__ == "__main__":
# pick some high port number here. Should probably put this into a file
# somewhere.
HOST, PORT = "localhost", 50010
server = None
try :
client(HOST, PORT, ' '.join(sys.argv))
print 'an insance was already open'
except socket.error :
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
if exceptionValue[0] == 111 :
print 'this is the first instance'
server = start_server(HOST, PORT)
else :
# don't actually know what happened ...
raise
app = SingleInstanceApp(server)
server.app = app
app.main()
The first execution of this script starts an asynchronous server on a predetermined port. This port is checked each time the script is run to see if another instance has already been started. If it has, the command line arguments are sent to the existing instance which can react to them however you want.
download single instance app. friend
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Pre-Query Samples
Validate the current query criteria or provide additional query criteria programmatically, just before sending the SELEC...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
"""
This will only spawn one gtk application at a time. If this command is executed
while an instance is already running, the command line arguments are sent to the
already running application.
"""
import sys
import pygtk
pygtk.require('2.0')
import gtk
import socket
import threading
import SocketServer
class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
data = self.request.recv(1024)
cur_thread = threading.currentThread()
# do something with the request:
self.server.app.label.set_label(data)
# could instead of the length of the input, could return error codes, more
# information (if the request was a query), etc. Using a length function
# as a simple example
response = 'string length: %d' % len(data)
print 'responding to',data,'with',response
self.request.send(response)
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
stopped = False
allow_reuse_address = True
def serve_forever(self):
while not self.stopped:
self.handle_request()
def force_stop(self):
self.server_close()
self.stopped = True
self.create_dummy_request()
def create_dummy_request(self):
client(self.server_address[0], self.server_address[1], 'last message for you')
def client(ip, port, message):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
sock.send(message)
response = sock.recv(1024)
print "Received: %s" % response
sock.close()
def start_server(host, port):
server = ThreadedTCPServer((host, port), ThreadedTCPRequestHandler)
ip, port = server.server_address
# Start a thread with the server -- that thread will then start one
# more thread for each request
server_thread = threading.Thread(target=server.serve_forever)
# Exit the server thread when the main thread terminates
server_thread.setDaemon(True)
server_thread.start()
return server
class SingleInstanceApp:
def destroy(self, widget, data=None):
self.server.force_stop()
gtk.main_quit()
#exit(1) # I'm sorry but mozembed is making a huge pain in my ass
def __init__(self, server):
self.server = server
# create a new window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_default_size(300,30)
self.window.connect("destroy", self.destroy)
self.label = gtk.Label("hello world")
self.window.add(self.label)
self.window.show_all()
# and the window
self.window.show()
def main(self):
gtk.gdk.threads_init()
gtk.main()
if __name__ == "__main__":
# pick some high port number here. Should probably put this into a file
# somewhere.
HOST, PORT = "localhost", 50010
server = None
try :
client(HOST, PORT, ' '.join(sys.argv))
print 'an insance was already open'
except socket.error :
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
if exceptionValue[0] == 111 :
print 'this is the first instance'
server = start_server(HOST, PORT)
else :
# don't actually know what happened ...
raise
app = SingleInstanceApp(server)
server.app = app
app.main()
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Pre-Query SamplesValidate the current query criteria or provide additional query criteria programmatically, just before sending the SELEC...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.