我有一个在根帐户下运行的容器,我可以开始使用它:
docker start containername
Run Code Online (Sandbox Code Playgroud)
我希望 crontab 启动它,所以我使用 root
crontab -e
Run Code Online (Sandbox Code Playgroud)
并设置一个像这样的条目:
* * * * * /usr/bin/docker start containername
Run Code Online (Sandbox Code Playgroud)
但这行不通。我也尝试过
* * * * * root /usr/bin/docker start containername
Run Code Online (Sandbox Code Playgroud)
没有运气。
有人知道我如何才能完成这项工作吗?
我在Grafana有几个仪表板,当我登录Grafana时,遇到Dashboard Not Found错误.
我想在登录Grafana时将其中一个Grafana仪表板设置为主页(默认页面).
我有以下代码:
import asyncio
async def test_1():
res1 = await foo1()
return res1
async def test_2():
res2 = await foo2()
return res2
if __name__ == '__main__':
print(asyncio.get_event_loop().run_until_complete([test_1, test_2]))
Run Code Online (Sandbox Code Playgroud)
但最后一次调用.run_until_complete()不起作用。如何使用 对多个任务执行异步调用.run_until_complete()?
python multithreading asynchronous python-3.x python-asyncio
这是我第一次使用石墨烯,对它没有很好的掌握。所以基本上是制作一个博客,用户可以在博客上点赞、评论和添加帖子到他最喜欢的,并互相关注。
我为所有用户操作制作了一个单独的模型
class user_actions(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
liked_post = models.ForeignKey(Post, related_name='post_likes',
on_delete=models.CASCADE)
liked_comments = models.ForeignKey(Comment,
related_name='comment_likes', on_delete=models.CASCADE)
fav = models.ForeignKey(Post, related_name='fav_post',
on_delete=models.CASCADE)
target = models.ForeignKey(User, related_name='followers',
on_delete=models.CASCADE, null=True, blank = True)
follower = models.ForeignKey(User, related_name='targets',
on_delete=models.CASCADE, null = True, blank = True)
def __str__(self):
return self.user.username
Run Code Online (Sandbox Code Playgroud)
因此,我对所有操作进行了更改,我正在尝试遵循 DRY 原则并将它们汇总在一起,我可能在这里做错了什么,新编码人员尽我所能:D
class UactionInput(InputObjectType):
liked_post_id = graphene.Int()
fav_post_id = graphene.Int()
comment_id = graphene.Int()
target_id = graphene.Int()
follower_id = graphene.Int()
class CreateUaction(graphene.Mutation):
user = graphene.Field(UactionType)
class Arguments:
input = UactionInput()
def mutate(self, info, …Run Code Online (Sandbox Code Playgroud) 我有两个Gitlab帐户.在我的旧帐户中,我添加了一个位于我的计算机上的ssh密钥~/.ssh/id_rsa.pub.
现在我想为我的新Gitlab帐户添加另一个ssh-key.如果没有ssh-keys冲突,我该怎么做?
对于发送字符串数据,以下代码有效:
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5667")
socket.send_string("my string data")
Run Code Online (Sandbox Code Playgroud)
对于发送图像(ndarray)以下代码有效:
def send_array(socket, img, flags=0, copy=True, track=False):
"""send a numpy array with metadata"""
md = dict(
dtype = str(img.dtype),
shape = img.shape,
)
socket.send_json(md, flags|zmq.SNDMORE)
return socket.send(img, flags, copy=copy, track=track)
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5667")
send_array(socket, my_ndarray_image )
Run Code Online (Sandbox Code Playgroud)
但我确实需要将字符串消息与图像文件一起发送。有没有办法在同一个请求中附加消息?
欢迎任何想法!谢谢
我正在使用 Django Rest Framework CreateAPIView 来创建评论。到目前为止一切都很好,这是我的代码。
楷模
class Posts(models.Model):
title = models.CharField(max_length=512, null=True)
slug = models.CharField(max_length=512, null=True)
class Comments(models.Model):
post = models.ForeignKey(Posts, on_delete=models.CASCADE)
content = models.CharField(max_length=5000, null=True)
Run Code Online (Sandbox Code Playgroud)
串行器
class CommentCreateSerializer(ModelSerializer):
class Meta:
model = Comments
fields = [
'content',
'post'
]
Run Code Online (Sandbox Code Playgroud)
并查看
class CommentCreateView(CreateAPIView):
permission_classes = [IsAuthenticated]
queryset = Comments.objects.all()
serializer_class = CommentCreateSerializer
Run Code Online (Sandbox Code Playgroud)
我向创建路线发送了一个帖子请求,post(ID)并且content一切正常。但问题是我想传递帖子slug而不是帖子 ID。
我不知道我该怎么做。我熟悉lookup_fields,但我不确定如何将它们应用于外键匹配。
我需要定期调用一个任务,但(a)等待时间几乎超过了这个时期。
在下面的代码中,如何do_something()在不需要await结果的情况下运行任务?
import asyncio
import time
from random import randint
period = 1 # Second
def get_epoch_ms():
return int(time.time() * 1000.0)
async def do_something(name):
print("Start :", name, get_epoch_ms())
try:
# Do something which may takes more than 1 secs.
slp = randint(1, 5)
print("Sleep :", name, get_epoch_ms(), slp)
await asyncio.sleep(slp)
except Exception as e:
print("Error :", e)
print("Finish :", name, get_epoch_ms())
async def main():
i = 0
while True:
i += 1
# Todo : this …Run Code Online (Sandbox Code Playgroud) 我创建了两个应用程序“myapi”和“minombre”,其中“minombre”将向“myapi”发出一个简单的 GET 请求,并将它们放入两个单独的 docker 容器中。运行“docker-compose up”后,容器运行,但api不传递数据。发出 GET 请求的“minombre”的views.py如下:
def index(request):
response = requests.get('http://myapi')
print(response)
data = response.json()
name = data['user']
message = data['message']
return HttpResponse('<h2> {} </h2> <br> <h5> {} </h5>'.format(name, message))
Run Code Online (Sandbox Code Playgroud)
这是我用来运行容器的 docker-compose.yml。
version: '3'
services:
myapi:
build: ./myapi
container_name: myapi
ports:
- "8001:8001"
command: python manage.py runserver 0.0.0.0:8001
minombre:
build: ./minombre
container_name: minombre
ports:
- "8000:8000"
command: python manage.py runserver 0.0.0.0:8000
depends_on:
- myapi
Run Code Online (Sandbox Code Playgroud)
这是例外:
Exception Type: ConnectionError
Exception Value:
HTTPConnectionPool(host='myapi', port=80): Max retries exceeded with url: / …Run Code Online (Sandbox Code Playgroud) 我试图承担角色以测试 CloudSearch API,但我被锁定了。
我懂了:
This app is blocked
The app tried to access sensitive info in your Google Account. To keep your account safe, Google blocked access.
Run Code Online (Sandbox Code Playgroud) python ×5
django ×3
asynchronous ×2
docker ×2
python-3.x ×2
cron ×1
git ×1
gitlab ×1
google-oauth ×1
grafana ×1
graphql ×1
pyzmq ×1
rest ×1
sockets ×1
ssh-keys ×1
ubuntu ×1
ubuntu-16.04 ×1
zeromq ×1