我试图通过 同时将项目插入到 postgres 表中ThreadedConnectionPool,但我不断收到psycopg2.pool.PoolError: trying to put unkeyed connection- 不知道为什么会发生这种情况。我也尝试过按顺序运行它,但仍然遇到相同的错误。
本质上,该代码会抓取网站的产品站点地图,并将抓取的项目插入数据库中。
代码:
class items:
def __init__(self):
self.conn = ThreadedConnectionPool(10, 100, dbname='postgres', user='xxx', password='xxx', host='xxx')
self.url = "some url"
self.session = requests.Session()
def scrape(self, pageNo):
//some logic
self.page(pageNo)
// scrapes specified page from sitemap
def page(self, page):
resp = self.session.get(self.mens+"?page="+str(page)).json()
products = resp['products']
ts = []
for item in products:
# self.indivProduct(self.url + pageNo)
t = threading.Thread(target=self.indivProduct, args=self.url + pageNo,))
ts.append(t)
t.start()
for item in ts:
item.join() …Run Code Online (Sandbox Code Playgroud) 我制作了一个 slack 应用程序,只需按一下按钮,就可以将负载发送到我的服务器。
当按钮被按下时,服务器会收到负载,其中包含一个 JSON 对象,其布局如下:
{
"type":"interactive_message",
"actions":[ .. ],
"callback_id":"wopr_game",
"team":{ .. },
"channel":{ .. },
"user":{ .. },
"action_ts":"1523126737.192039",
"message_ts":"1523126734.000016",
"attachment_id":"1",
"token":"aYydBrSjjHHz4UqYXKB4tzDZ",
"is_app_unfurl":false,
"original_message":{ .. },
"response_url":"https://hooks.slack.com/actions/T1ABCD2E12/330361579271/0dAEyLY19ofpLwxqozy3firz",
"trigger_id":"342463876993.134749426887.e0c3b2e25d3a070b66361526a13be0bf"
}
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法访问 JSON 对象中的任何变量。这是我针对特定请求的 express js 代码。
router.post('/', function(req, res, next) {
console.log(req.body['payload']) // prints json obj fine
console.log(req.body['payload']['response_url']) // undefined
res.send('Hello');
});
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么吗?
目前,我有数十万个整数 ID 的集合,并且我正在执行以下任务(假设该集合cache现在存储在列表中):
cache = list()
# lets say this cache is populated
for x in range(0,1000000):
if x not in cache:
#do_something
Run Code Online (Sandbox Code Playgroud)
对于我来说,使用列表来搜索not in某些内容的成本有多高?我会从使用另一种数据结构中受益吗?如果是的话,哪种数据结构最好?
我想知道上述情况是否可行.我目前有一个字典列表(列表中的字典数是arbritary).我将不得不将列表中的字典"复制"到另一个字典中,该字典将成为我的HTTP post请求的有效负载.下面的例子应该更清楚:
myList = [{'updates[123]': '1'}, {'updates[234]': '2'}, {'updates[345]': '3'}]
Run Code Online (Sandbox Code Playgroud)
然后我需要将它复制到另一个词典中.
payload = {
'updates[123]': '1',
'updates[234]': '2',
'updates[345]': '3'
}
Run Code Online (Sandbox Code Playgroud)
是否有可能创建"有效载荷"字典而不知道原始列表中的元素数量,或者是否只有在其他情况下检查len(myList)以便索引正确的次数的唯一方法?
谢谢
假设我有一个界面:
type Module interface {
Run(moduleInput x) error // x is the type of moduleInput
}
Run Code Online (Sandbox Code Playgroud)
其中每个“模块”将完成该Run功能。但是,它moduleInput不是单个结构 - 它应该能够接受任何结构,但只接受允许的结构,即不接受interface{}(例如,仅moduleAInputs和moduleBInputs结构)。
理想情况下,Run每个模块的函数都具有以下类型,moduleXInput其中 X 是示例模块。
是否可以使用泛型或其他方式限制 的类型moduleInput?