2011-11-17 22 views
5

मैं चैट सर्वर के साथ इंटरफेस करने के लिए एक साधारण पायथन स्क्रिप्ट लिखने की कोशिश कर रहा हूं। यह अद्यतन के लिए सर्वर को मतदान करेगा, और सर्वर को चैट के रूप में सर्वर भेजने के लिए पाठ दर्ज करने की अनुमति देगा। मैं मल्टीथ्रेडिंग के साथ मिलकर कुछ हैक कर सकता हूं, लेकिन यह भयानक लग रहा है। क्या उपयोगकर्ता इनपुट स्वीकार करते समय स्क्रीन पर अद्यतन जानकारी प्रदर्शित करने का एक अच्छा, सरल तरीका है? मैं बिना शाप के इसे करना पसंद करूंगा।असिंक्रोनस इंटरैक्टिव पायथन स्क्रिप्ट

+1

एक जैसे दो कार्यों को करने के लिए, आपको धागे की आवश्यकता है। उपयोगकर्ता इनपुट, शायद मुख्य धागा को संभालने के लिए आपको धागा होना चाहिए। फिर चैट सर्वर से आने वाले प्रतिक्रियाओं को संभालने के लिए एक और धागा बनाएं। –

+0

मुझे वह हिस्सा मिल रहा है, लेकिन यह सही प्रदर्शित नहीं करता है। मैं इनपुट प्राप्त करने के लिए raw_input का उपयोग कर रहा हूं, और जब आप इसे कॉल करने के बाद टेक्स्ट प्रिंट करते हैं तो यह गड़बड़ हो जाता है। –

+2

@HunterMcMillen - बिल्कुल सही नहीं - कुछ (जी) यूआई टूलकिट्स और संचार ढांचे एक गैर-थ्रेडेड लूप का उपयोग करते हैं (अक्सर 'चयन' पर आधारित), उदाहरण के लिए। पीईजीटीके और ट्विस्टेड। – detly

उत्तर

0

डिस्प्ले विंडो/स्क्रीन को एक अस्थायी स्थानीय डीबी द्वारा संचालित करने दें।

कोड 1: डीबी से स्क्रीन अपडेट करता है। आप के साथ या एक ठहराव (स्क्रीन ताज़ा दर) के बिना

कोड 2 अनंत लूप का उपयोग कर सकते हैं: अपडेट db पहले नहीं उपयोगकर्ता कुछ में प्रवेश करती है

कोड 3: लगातार चैट सर्वर से अद्यतन के लिए जाँच और अद्यतन करता है डीबी जल्द ही कोई नया राहत प्राप्त नहीं होती है।

0

मुझे नहीं पता कि कैसे ncurses के साथ कोड करना है, लेकिन यहां wxwidget के साथ एक समाधान है। यह डिजाइन के संबंध में मोटे तौर पर समान होना चाहिए।

"""Asynchronous interactive Python script""" 
import random 
import threading 
import wx 
import wx.lib.mixins.listctrl as listmix 

LOCK = threading.Lock() 

def threadsafe(function): 
    """A decorator that makes a function safe against concurrent accesses.""" 
    def _decorated_function(*args, **kwargs): 
     """Replacement function.""" 
     with LOCK: 
      function(*args, **kwargs) 
    return _decorated_function 


class SharedList(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin): 
    """An output list that can print information from both the user and the server. 

    N.B.: The _print function that actually updates the list content uses the threadsafe decorator. 
    """ 

    def __init__(self, parent, pos=wx.DefaultPosition, size=(-1, -1), style=wx.LC_REPORT): 
     wx.ListCtrl.__init__(self, parent, wx.ID_ANY, pos, size, style) 
     self.InsertColumn(0, 'Origin', width=75) 
     self.InsertColumn(1, 'Output') 
     listmix.ListCtrlAutoWidthMixin.__init__(self) 
     self.resizeLastColumn(1000) 
     self._list_index = 0 

    def user_print(self, text): 
     """Print a line as the user""" 
     self._print("user", text) 

    def chat_print(self, text): 
     """Print a line as the chat server""" 
     self._print("chat", text) 

    @threadsafe 
    def _print(self, origin, text): 
     """Generic print function.""" 
     self.InsertStringItem(self._list_index, str(origin)) 
     self.SetStringItem(self._list_index, 1, str(text)) 
     self.EnsureVisible(self.GetItemCount() - 1) 
     self._list_index = self._list_index + 1 


class ServerChecker(threading.Thread): 
    """A separate thread that would connect to the IRC chat.""" 

    def __init__(self, shared_list): 
     threading.Thread.__init__(self) 
     self._stop_event = threading.Event() 
     self._shared_list = shared_list 

    def run(self): 
     """Connection to the server, socket, listen, bla bla bla.""" 
     while not self._stop_event.is_set(): 
      self._shared_list.chat_print("bla bla bla") 
      self._stop_event.wait(random.randint(1, 3)) 

    def stop(self): 
     """Stop the thread.""" 
     self._stop_event.set() 


class SampleFrame(wx.Frame): 
    """The main GUI element.""" 

    def __init__(self): 
     super(SampleFrame, self).__init__(parent=None, title='DAS Board', size=(600, 400)) 
     self._shared_list = None 
     self._user_text = None 
     self._init_ui() 
     self._thread = ServerChecker(self._shared_list) 
     self._thread.start() 
     self.Bind(wx.EVT_CLOSE, self._on_close) 
     self.Center() 
     self.Show() 

    def _init_ui(self): 
     """Building and assembling the graphical elements. 
     Don't pay too much attention, especially if you want to use ncurses instead. 
     """ 
     panel = wx.Panel(self) 
     self._shared_list = SharedList(panel) 
     main_box_v = wx.BoxSizer(wx.VERTICAL) 
     main_box_v.Add(self._shared_list, proportion=1, flag=wx.EXPAND|wx.ALL, border=10) 
     self._user_text = wx.TextCtrl(panel, -1, value="User text to send...", 
             style=wx.TE_CENTRE) 
     main_box_v.Add(self._user_text, proportion=0, flag=wx.EXPAND|wx.ALL, border=10) 
     button = wx.Button(panel, label="Send user text.") 
     button.Bind(wx.EVT_BUTTON, self._user_send_text) 
     main_box_v.Add(button, flag=wx.EXPAND|wx.ALL, border=10) 
     panel.SetSizer(main_box_v) 

    def _user_send_text(self, event): 
     """Button callback""" 
     self._shared_list.user_print(self._user_text.GetValue()) 
     event.Skip() 

    def _on_close(self, event): 
     """Stop the separate thread, then destroy the GUI.""" 
     event.Skip() 
     self._thread.stop() 
     self.Destroy() 


APP = wx.App(0) 
SampleFrame() 
APP.MainLoop() 
संबंधित मुद्दे