Ubuntu - 18.04
Python - 3.6.6
cx_Freeze - 6.1
简单的main_script.py文件(存储库中的示例 - https://github.com/Yuriy-Leonov/cython_multiprocessing_issue)
import multiprocessing
if __name__ == '__main__':
print("step-1")
multiprocessing.set_start_method("spawn")
print("step-2")
multiprocessing.freeze_support()
print("step-3")
manager = multiprocessing.Manager()
print("step-4")
s_dict = manager.dict()
print("finish")
Run Code Online (Sandbox Code Playgroud)
和setup.py(用于 cx_Freeze):
import cx_Freeze
executables = [cx_Freeze.Executable("main_script.py")]
cx_Freeze.setup(
name="Example",
options={
"build_exe": {
"replace_paths": [("*", "")]
},
},
executables=executables
)
Run Code Online (Sandbox Code Playgroud)
通过命令构建可执行文件后,python setup.py build我运行了它,控制台日志包含以下内容:
step-1
step-2
step-3
step-1
step-2
step-3
step-1
step-2
step-3
...
Run Code Online (Sandbox Code Playgroud)
并且产生无限的过程。
我知道multiprocessing.Manager()应该产生“服务器”进程。但无法获得当前行为的线索以及如何强制它创建“共享字典”
multiprocessing.set_start_method("spawn") 由于主程序行为,无法更改和要求。 …
鉴于:
- Ubuntu
- py2exe和pyinstaller - 使用setup.py(或其他)的Python脚本
from distutils.core import setup
import py2exe
import os
setup(
version = "1.0",
description = 'foo',
url = "",
name = "foo",
console=[{
"script":"main.py",
"dest_base":"foo",
}],
zipfile = "shared.dll",
options = {"py2exe":{
'bundle_files': 1,
'optimize': 2,
"dll_excludes": ['MSVCP90.dll', 'msvcr71.dll', "IPHLPAPI.DLL", "NSI.dll", "WINNSI.DLL", "WTSAPI32.dll"],
"includes": ["utils"]
}}
)
Run Code Online (Sandbox Code Playgroud)
需要:
- 一个.exe文件,也许一些.dll(我真的不知道)
我做的步骤:
- 设置pip3和python 3.4(https://askubuntu.com/questions/524399/issues-with-py2exe)
- 为ubuntu安装py2exe"pip3安装py2exe"
- 运行"python3.4 setup.py py2exe "并得到以下追溯:
Traceback (most recent call last):
File "setup.py", line 2, in <module> …Run Code Online (Sandbox Code Playgroud) Python:3.6.6
pyglet版本:1.3.2
abstract_model.py
import pyglet
def get_texture_group(file, order_group_index):
texture = pyglet.image.load(file).texture
order_group = pyglet.graphics.OrderedGroup(order_group_index)
return pyglet.graphics.TextureGroup(texture, order_group)
class AbstractModel(object):
def _create_as_vertex(self):
v_x = self.cell_data.get("x") * 32
v_y = self.cell_data.get("y") * -1 * 32
texture_group = self.map_type_iamge.get(self.cell_data.get("t"))
x_offset = self.x_offset * self.scale
x, y, z = v_x + x_offset, v_y, self.z
x_ = (texture_group.texture.width * self.scale + x_offset + v_x)
y_ = (texture_group.texture.height * self.scale + v_y)
tex_coords = ('t2f', (0, 0, 1, 0, 1, 1, 0, 1))
self.vertices …Run Code Online (Sandbox Code Playgroud) Python 3.6.6
这是代码:
import asyncio
import time
from concurrent.futures import ProcessPoolExecutor
executor_processes = ProcessPoolExecutor(2)
def calculate():
while True:
print("while")
time.sleep(1)
async def async_method():
loop_ = asyncio.get_event_loop()
loop_.run_in_executor(executor_processes, calculate)
await asyncio.sleep(1)
print("finish sleep")
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(async_method())
print("main_thread is finished")
Run Code Online (Sandbox Code Playgroud)
输出:
while
finish sleep
main_thread 已完成
while
while
...
我希望子进程将被终止,就像当使用守护进程属性生成进程时一样:
import asyncio
import time
import multiprocessing
def calculate():
while True:
print("while")
time.sleep(1)
async def async_method():
proc = multiprocessing.Process(target=calculate)
proc.daemon = True
proc.start()
await asyncio.sleep(1)
print("finish sleep")
if …Run Code Online (Sandbox Code Playgroud) 如何在Javascript上以new Date()。toUTCString()的形式获取python结果?
在JavaScript上我做:
new Date().toUTCString()
"Tue, 08 Sep 2015 09:45:32 GMT"
Run Code Online (Sandbox Code Playgroud)
在Python上
import datetime # or time, or either
date = ??? # some code
print date # >>> "Tue, 08 Sep 2015 09:45:32 GMT"
Run Code Online (Sandbox Code Playgroud) 我有两个类(结构示例):
public class A {
protected void doCheck(stuff) throw CommonException, A1Exception{
if(stuff==1){
throw new A1Exception(stuff);
}
throw new CommonException(stuff);
}
public void invokeAstuff(stuff) throw CommonException, A1Exception{
doCheck(stuff);
}
}
public class B {
protected void doCheck(stuff) throw CommonException, B1Exception{
if(stuff==1){
throw new B1Exception(stuff);
}
throw new CommonException(stuff);
}
public void invokeBstuff(stuff, otherStuff) throw CommonException, B1Exception{
doCheck(stuff);
}
}
Run Code Online (Sandbox Code Playgroud)
"doCheck"方法具有相同的处理内容的逻辑,但是每个类都抛出一个不同的异常:A类的A1Exception和B类的B1Exception
我的问题是:如何编写将使用通用逻辑实现"doCheck"的基本类+在某些条件下应该抛出的异常类?A和B应该扩展这个类.
这是示例测试:
import a
import b
import c
import mock
from django.test import TestCase
@mock.patch.object(a, "method_a")
@mock.patch.object(b, "method_b")
@mock.patch.object(c, "method_c")
class SomeTestCase(TestCase):
def setUp(self):
# I want to set mock_method_a.return_value = 1 once here (or not here, but once)
pass
def test_one(self, mock_method_a, mock_method_b, mock_method_c):
mock_method_a.return_value = 1
mock_method_b.return_value = 2
pass # some test stuff
def test_two(self, mock_method_a, mock_method_b, mock_method_c):
mock_method_a.return_value = 1
mock_method_b.return_value = 2
pass # some test stuff
def test_three(self, mock_method_a, mock_method_b, mock_method_c):
mock_method_a.return_value = 1
mock_method_b.return_value …Run Code Online (Sandbox Code Playgroud) 这是一个代码笔示例https://codepen.io/yury-leonov/pen/rZxxQE
$(".example").on("click", function(e){
$(e.target).toggleClass("reverse");
})Run Code Online (Sandbox Code Playgroud)
.reverse{
transform: scaleX(-1);
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="width: 700px;height: 700px;margin-left: 100px">
<svg viewBox="-200 0 700 700">
<image class="example" href="https://i.pinimg.com/originals/6f/3d/6a/6f3d6aab605e25af947d804c4a2cb558.jpg" width="150px" height="150px" x="50", y="50"/>
<image class="example" href="https://i.pinimg.com/originals/6f/3d/6a/6f3d6aab605e25af947d804c4a2cb558.jpg" width="150px" height="100px" x="100", y="0"/>
<!--x value can be changed during time-->
</svg>
</div>Run Code Online (Sandbox Code Playgroud)
箭头从其位置移动
箭头反了。留在同一个地方。仅基于 css 执行(无需 js 计算最终 x 值并进行设置)
translateX(-???px) 的硬编码不是一个选项,因为可能有许多对象应该被反转。
$ ruby --version
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]
Run Code Online (Sandbox Code Playgroud)
python 上有一个例子:
def any_func(a, b=2, c = 0, d=None):
return c
print(any_func(25, c=30)) # will print 30
Run Code Online (Sandbox Code Playgroud)
我怎样才能在红宝石上做同样的事情?
ruby 上如何调用“kwargs”?我想知道它以便进一步搜索。
我在 ruby 上尝试了以下代码:
#!/usr/bin/env ruby
def any_func(a, b = 2, c = 0, d = nil)
c
end
puts any_func(25, c = 30) # will print 0, 30 is expected
Run Code Online (Sandbox Code Playgroud)