2014-10-31 3 views
9

asyncio 및 Gtk +와 관련된 Okey 관련 질문. Gtk.main 루프에서 아래 코드를 어떻게 실행합니까? 예를 찾아 보았지만 찾을 수 없습니다.Gtk 메인 루프에서 실행중인 Asyncio 호출

#!/usr/bin/python3.4 

import asyncio 

@asyncio.coroutine 
def client_connected_handler(client_reader, client_writer): 
    print("Connection received!") 
    client_writer.write(b'Hello') 
    while True: 
     data = yield from client_reader.read(8192) 
     if not data: 
      break 
     if 'EXIT' in data.decode(): 
      print("Closing server") 
      break 
     print(data) 
     client_writer.write(data) 
    print('Server is closed') 


loop = asyncio.get_event_loop() 
Server=asyncio.start_server(client_connected_handler, 'localhost', 2222) 
server=loop.run_until_complete(Server) 
loop.run_forever() 

편집 :

좋아요 나는 gbulb 내의 experiance을 작성해야합니다. 먼저 pip3를 사용하여 검색했습니다. 나는 그것을 발견하고 그것을 설치하려고 시도했으나 잘못된 링크로 인해 설치에 슈퍼 유저를 사용하지 못했습니다. 다음 나는 그들의 저장소에서 그것을 다운로드하고 그것을 설치했다. 나는 이것을 얻었다 example 나는 그것을 실행하고 핵심 모듈의 누락 된 인수에 대한 몇 가지 오류가있어. 난 정말 어떤 오류가 다른 PC에서 이것을 쓰는 원인인지 모르겠다. 업데이트는 최대한 빨리 이루어집니다. 또한 다른 사람들이 테스트 할 수 있다면 감사 할 것입니다.

+0

잘 작동 gbulb''pip3 설치, 위의를 업데이트하고, 카운터 데모 : 장소에이 수정

, 모두 들어오는 연결을 승인하고 실행하는 데 귀하의 예제를 수정하는 GTK 간단하다 [Github] (https://github.com/nathan-hoad/gbulb)의 예제 섹션은 문제없이 실행됩니다. – jcoppens

답변

9

gbulb library은 asyncio 메인 루프 인터페이스와 GLib 메인 루프 구현 간의 커넥터입니다. 그러나 gbulb의 현재 마스터는 Python 3.4와 함께 asyncio에 대해 손상되었습니다. 이 문제를 해결하려면 마스터가 아닌 this fork을 확인하십시오.

#!/usr/bin/python3.4 

import asyncio, gbulb 
from gi.repository import Gtk 
asyncio.set_event_loop_policy(gbulb.GtkEventLoopPolicy()) 

@asyncio.coroutine 
def client_connected_handler(client_reader, client_writer): 
    print("Connection received!") 
    client_writer.write(b'Hello') 
    while True: 
     data = yield from client_reader.read(8192) 
     if not data: 
      break 
     if 'EXIT' in data.decode(): 
      print("Closing server") 
      break 
     print(data) 
     client_writer.write(data) 
    print('Server is closed') 


loop = asyncio.get_event_loop() 
Server=asyncio.start_server(client_connected_handler, 'localhost', 2222) 
server=loop.run_until_complete(Server) 

w = Gtk.Window() 
w.add(Gtk.Label('hey!')) 
w.connect('destroy', Gtk.main_quit) 
w.show_all() 

loop.run_forever()