import asyncio
async def async_io():
print("async_io start")
result = 100
print("async_io end")
return result
async def async_db_query(data):
print("async_db_query start")
result = await asyncio.sleep(1, result=500)
print("async_db_query end")
return result
async def async_calculating(data):
print("async_calculating start")
res = 0
for i in range(1000):
res += 1
print("async_calculating end")
return res
async def task():
print("Task start")
res_async = await async_io()
res = await asyncio.gather(
async_db_query(res_async),
async_calculating(res_async)
)
task_calc = sum(res)
print(f"Task end. Result: {task_calc}")
return task_calc
loop = asyncio.get_event_loop()
loop.create_task(task())
loop.run_forever()
package main
import (
"encoding/json"
"fmt"
)
type TemplateCategory struct {
ID int `json:"id"`
Name string `json:"name"`
}
type Template struct {
Name string `json:"name"`
CategoryInfoRaw *json.RawMessage `json:"category_info"`
CategoryInfo *TemplateCategory
}
func (t *Template) UnmarshalJSON(data []byte) error {
type template Template
if err := json.Unmarshal(data, (*template)(t)); err != nil {
return err
}
if t.CategoryInfoRaw == nil || string(*t.CategoryInfoRaw) == "[]" {
t.CategoryInfo = nil
} else {
if err := json.Unmarshal(*t.CategoryInfoRaw, &t.CategoryInfo); err != nil {
return err
}
}
return nil
}
func main() {
res := &Template{}
str1 := `{
"name": "Мой шаблон",
"category_info": {
"id": 109,
"name": "Тест"
}
}`
str2 := `{
"name": "Мой шаблон",
"category_info": []
}`
_ = json.Unmarshal([]byte(str1), res)
fmt.Printf("%+v\n", res)
fmt.Printf("%+v\n", res.CategoryInfo)
_ = json.Unmarshal([]byte(str2), res)
fmt.Printf("%+v", res)
}
greet = wrapper
, поэтому, когда происходит вызов greet(), вместо этого вызывается wrapper(), а не определенный в изначальном коде greet. Какой chat_id надо указывать в телеграмм боте?
FAQ
Can I use celery? -- at the moment the answer is no. We're working on it.
Can I just use threads? -- we don't support threads in web apps, no