using asyncio in python_使用asyncio.start_server时,python中的异步服务器代码不起作用(async server code in python wont...

使用asyncio.start_server时,python中的异步服务器代码不起作用(async server code in python wont work when using asyncio.start_server)

这是异步python,但是当我向它发送请求并使用time.sleep(5)进行并行测试时它将不起作用,但它是顺序的,并且顺序执行请求:

def main(*args):

loop = get_event_loop()

coro = asyncio.start_server(handle_echo, '127.0.0.1', 50008,loop=loop)

srv = loop.run_until_complete(coro)

def handle_echo(reader, writer):

data = yield from reader.read(500)

message = data.decode(encoding='utf-8')

nameindex=('name="calculator2"' in message)

if nameindex:

time.sleep(5)

writer.write("Content-Length: 1\r\n\r\n2".encode())

yield from writer.drain()

else:

writer.write("Content-Length: 1\r\n\r\n1".encode())

yield from writer.drain()

print("Close the client socket")

writer.close()

This is async python, but won't work when I sending requests to it and use time.sleep(5) for test of parallel, but it's sequential and requests executed sequentially:

def main(*args):

loop = get_event_loop()

coro = asyncio.start_server(handle_echo, '127.0.0.1', 50008,loop=loop)

srv = loop.run_until_complete(coro)

def handle_echo(reader, writer):

data = yield from reader.read(500)

message = data.decode(encoding='utf-8')

nameindex=('name="calculator2"' in message)

if nameindex:

time.sleep(5)

writer.write("Content-Length: 1\r\n\r\n2".encode())

yield from writer.drain()

else:

writer.write("Content-Length: 1\r\n\r\n1".encode())

yield from writer.drain()

print("Close the client socket")

writer.close()

原文:https://stackoverflow.com/questions/42649846

2020-07-16 10:07

满意答案

更换:

time.sleep(5)

有:

await asyncio.sleep(5)

replace:

time.sleep(5)

with:

await asyncio.sleep(5)

2017-03-07

相关问答

对create_server的调用返回一个协同程序。 调用run_until_complete时,将返回具有sockets属性的服务器实例。 更改startListening的定义以将startListening的返回值保存为self.server并修改getPortNumber定义以使用它。 您还需要将getpeername更改为getsockname以获取分配的端口。 class NodeServer:

def __init__(self, hostNode):

sel...

协议代码包含一个拼写错误,它命名方法data_deceived而不是data_received 。 方法名称更正后,输出变得与阻塞示例中的相同。 在协程版本中,问题在于reader.read()指示asyncio在数据结束之前收集数据,并在准备就绪时提供数据。 将其更改为reader.read(1024) ,类似于阻塞版本中的sock.recv(1024) ,修复了代码。 The protocol code contains a typo, it names the method data_dec...

您缺少与Key_Code_List表的连接中的Unit_ID字段。 SELECT DISTINCT

U.Unit_ID, P.Plant_ID, P.Project_NR, U.Key_code_list,

S.Status_type, Kc.Key_codes4

FROM

Plant as P

INNER JOIN

Unit as U ON P.Plant_NR = U.Plant_NR

INNER JOIN

[dbo].[Key_code_l...

如果您想开始使用不支持它的库编写asyncio代码,可以使用BaseEventLoop.run_in_executor 。 这允许您向ThreadPoolExecutor或ProcessPoolExecutor提交一个可调用对象,并异步获取结果。 默认执行程序是一个由5个线程组成的线程池。 例: # Python 3.4

@asyncio.coroutine

def some_coroutine(*some_args, loop=None):

while True:

[.....

这里是一个简单的代理,它允许你wget 127.0.0.1:8888并从google获得html响应: import asyncio

class Client(asyncio.Protocol):

def connection_made(self, transport):

self.connected = True

# save the transport

self.transport = transport

def data_...

我想永远不要直接使用传输/协议对。 asyncio具有用于高级编程的Streams API。 客户端代码可能如下所示: @asyncio.coroutine

def communicate():

reader, writer = yield from asyncio.open_connection(HOST, PORT)

writer.write(b'data')

yield from writer.drain()

answer = yield from reade...

以下脚本对Python 2或3执行相同的操作: try:

# Python 2

from SimpleHTTPServer import test

except ImportError:

# Python 3

from http.server import test

test()

这将运行与使用-m开关从命令行运行模块时使用的完全相同的可调用 。 Python 3版本包括命令行支持,以确定要绑定到哪个接口和端口,但您的命令行无论如何都不会使用它。 The fol...

我终于通过在端口80上运行python服务器解决了这个问题。我认为这是允许来自外部的流量的默认端口。 这对我来说现在很有用。 I finally solved this by running the python server on port 80. It is the default port to let traffic from the outside in, I think. This worked for me for now.

这不会stdin_q.get() ,因为对stdin_q.get()的调用将阻止您的事件循环。 这意味着如果您的服务器有多个客户端,那么所有这些客户端都会被首先到达stdin_q.get()所有客户端完全阻止,直到您将数据发送到队列中。 解决这个问题的最简单方法是使用BaseEvent.loop.run_in_executor在后台ThreadPoolExecutor运行stdin_q.get ,它允许您在不阻塞事件循环的情况下等待它: @asyncio.coroutine

def get_inp...

相关文章

转自:http://www.cnblogs.com/Amaranthus/archive/2011/0

...

用java编写的一个socket服务端,通过一个tcp测试工具测试这个服务端,发现发送数据给服务端,服

...

mod_python: the long story - Grisha Trubetskoy

...

python里的字典就像java里的HashMap,以键值对的方式存在并操作,其特点如下:通过键来存取

...

OpenCms默认是以内嵌的Solr作为全文搜索服务的,不过我们也可以配置一个独立的Solr搜索服务器

...

网上已经有很多关于Hadoop源码分析的好文,在这记录的目的是把自己在看Hadoop源码时自己的一些体

...

erver-Sent Events - 单向的信息处理.一个SSE(server send event

...

我的weblogic启动时时报server错误,如下: <2011-11-23 下午03时56分

...

说明:Hadoop版本:hadoop-1.2.1.tar.gz。linux系统12.04,不过这里跟系

...

http://halfhalf.posterous.com/dont-work-be-hated-lo

...

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值