latyas
2015-12-01 11:22:15 +08:00
问题略有问题,要想知道是不是 python 的坑,参考如下代码
单连接单线程的情况
```python
import socket
import time
conn = socket.socket()
conn.connect(('127.0.0.1', 6379))
_ = time.time()
for i in range(100000):
#print(i)
foo = 'ksljdfldksjfklsldkfjkdsjflkdsjflkdsklskdjfljewl' + str(i)
bar = 'ksljdfldksjfklsldkfjkdsjflkdsjflkdsklskdjfljewl'
command = 'SET %s %s\r\n' % (foo, bar)
conn.send(command.encode('utf-8'))
print('total', time.time() - _)
```
```shell
total 0.10007500648498535
```
开 10 个 worker 的进程池, 10 个 tcp 链接
```python
import socket
import time
import multiprocessing
def process(n):
conn = socket.socket()
conn.connect(('127.0.0.1', 6379))
for i in range(10000):
#print(i)
foo = 'ksljdfldksjfklsldkfjkdsjflkdsjflkdsklskdjfljewl' + str(i)
bar = 'ksljdfldksjfklsldkfjkdsjflkdsjflkdsklskdjfljewl'
command = 'SET %s %s\r\n' % (foo, bar)
conn.send(command.encode('utf-8'))
pool = multiprocessing.Pool(10)
_ = time.time()
pool.map(process, range(10))
print('total', time.time() - _)
```
```shell
total 0.029608488082885742
```
如果这么慢,是不是驱动上有坑?