我想在app.py上创建两个带有asyncio的协议(TcpClient和UdpServer),其中TcpClient将与server.py和UdpServer作为UDP服务器建立持久连接:
我需要的是:
a)两种通信协议:相互调用方法.这只是在第一个连接上工作.如果TcpClient重新连接,则无法再次发送字符串"send to tcp".来自UdpServer.我检查print(self)
并且TcpClient创建一个新实例,旧的仍然存在但没有连接,但我不知道如何重构.我认为我以错误的方式使用asyncio.
b)当TcpClient与server.py断开连接时,等待5s并再次尝试重新连接,依此类推.我试图使用call_later()
asyncio,但我认为有一种本地方式来做到这一点,而不是一种技巧.
c)当我启动app.py并且如果TcpClient无法连接时,我想尝试在5s后重新连接,依此类推.我不知道该怎么做.
这是我的app.py server.py的示例测试.server.py仅用于测试 - 这将是另一种语言.
只是说我尝试了什么:
1)当我启动app.py并且server.py关闭时,app.py不会重试.
2)当app.py连接到server.py并且服务器关闭并快速启动时,TcpClient重新连接,但我无法在新实例上相互连接方法并发送字符串"send to tcp".到server.py,只是旧的,没有更多的连接.
3)如果我用asyncio.async()
,而不是run_until_complete()
我不能打电话相互协议的方法.
我把app.py和server.py放在这里,所以你可以复制并运行测试.
我ncat localhost 9000 -u -v
用来发送字符串"send to tcp.".此字符串需要打印在UdpServer类上,并传递给TcpClient类上的方法send_data_to_tcp,此方法将字符串发送到server.py.< - 首次重新连接tcpClient后,这不起作用.
我正在使用Python 3.4.0.
非常感谢.
import asyncio
#TCP client
class TcpClient(asyncio.Protocol):
message = 'Testing'
def connection_made(self, transport):
self.transport = transport
self.transport.write(self.message.encode())
print('data sent: {}'.format(self.message))
server_udp[1].tcp_client_connected()
def data_received(self, data):
self.data = format(data.decode())
print('data received: {}'.format(data.decode()))
if self.data == 'Testing':
server_udp[1].send_data_to_udp(self.data)
def send_data_to_tcp(self, data):
self.transport.write(data.encode())
def …
Run Code Online (Sandbox Code Playgroud) 在下面的语言大号不可判定?
L = { M | M是图灵机描述,并且存在长度为k的输入x,使得M在最多k步之后停止}
我想是的但是我无法证明这一点.我试着想到减少停止问题.
我正在尝试创建一个子进程,然后将SIGINT发送给子进程而不终止父进程.我试过这个:
pid=fork();
if (!pid)
{
setpgrp();
cout<<"waiting...\n";
while(1);
}
else
{
cout<<"parent";
wait(NULL);
}
Run Code Online (Sandbox Code Playgroud)
但当我点击Cc时,两个过程都被终止了
我有一个巨大的结构,有不同类型的成员.我知道成员根据他们的类型对齐.
无论如何都要找出差距在哪里?
我的意思是我有兴趣知道编译器在哪些成员之间插入了一个间隙(当然差距是多少).