1
timonwong 2014-05-13 15:52:06 +08:00
Quick and dirty way:
GUI loop在main thread上 I/O loop放到另外一个thread上 注意io loop中不要直接操作GUI,委托到main thread上操作 |
2
jander OP 在一个thread里放io loop,单独启动一个Server或者Client没问题,
但是,如果我要在一个thread里放io loop,并启动一个Server,然后在一个Button注册一个事件,开一个线程去调用一个coroutine是不行的,出现错误: AssertionError: There is no current event loop in thread 'Thread-3'. 类似这样: ``` def hello(): def _x(): @asyncio.coroutine def echo(): words = 'hello' reader, writer = yield from asyncio.open_connection('localhost', 8888) writer.write(words.encode('utf-8')) answer = yield from reader.readline() print(answer.decode('utf-8')) writer.close() loop = asyncio.new_event_loop() loop.run_until_complete(echo()) loop.close() threading.Thread(target=_x).start() ``` |
3
jander OP |