关于一个乘法器
def karatsuba(num1, num2):
num1Str, num2Str = str(num1), str(num2)
if num1Str[0] == '-': return -karatsuba(-num1, num2)
if num2Str[0] == '-': return -karatsuba(num1, -num2) # 显然是来判断正负的,不过我很好奇为什么是两个负号,那岂不就白费了吗?
if num1 < 10 or num2 < 10: return num1 * num2
maxLength = max(len(num1Str), len(num2Str))
num1Str = ''.join(list('0' * maxLength)[:-len(num1Str)] + list(num1Str))
# Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。
num2Str = ''.join(list('0' * maxLength)[:-len(num2Str)] + list(num2Str))
splitPosition = maxLength // 2
high1, low1 = int(num1Str[:-splitPosition]), int(num1Str[-splitPosition:])
high2, low2 = int(num2Str[:-splitPosition]), int(num2Str[-splitPosition:])
z0, z2 = karatsuba(low1, low2), karatsuba(high1, high2)
z1 = karatsuba((low1 + high1), (low2 + high2))
return z2 * 10 ** (2 * splitPosition) + (z1 - z2 - z0) * 10 ** splitPosition + z0
c = karatsuba(12345678, 87654321)
print(c)
关于gevent socket redis
from gevent import socket as gsocket
import gevent
import redis
import redis.connection
redis.connection.socket = gsocket
r = redis.StrictRedis()
p = r.connection_pool
gevent超时参数
def with_timeout(seconds, function, *args, **kwds):
"""Wrap a call to *function* with a timeout; if the called
function fails to return before the timeout, cancel it and return a
flag value, provided by *timeout_value* keyword argument.
If timeout expires but *timeout_value* is not provided, raise :class:`Timeout`.
Keyword argument *timeout_value* is not passed to *function*.
"""
timeout_value = kwds.pop("timeout_value", _NONE)
timeout = Timeout.start_new(seconds, _one_shot=True)
# def start_new(cls, timeout=None, exception=None, ref=True, _one_shot=False):
try:
try:
return function(*args, **kwds)
except Timeout as ex:
if ex is timeout and timeout_value is not _NONE:
return timeout_value
raise
finally:
timeout.cancel()
一些杂项
版权声明:除特别注明外,本站所有文章均为王晨曦个人站点原创
转载请注明:出处来自王晨曦个人站点 » gevent socket timeout karatsuba乘法器
出发地 adhiogsxcig [url=http://www.gv2av8406lfr67aym453d4cy409b2cp2s.org/]udhiogsxcig[/url] dhiogsxcig http://www.gv2av8406lfr67aym453d4cy409b2cp2s.org/