小编kkk*_*kv_的帖子

当父进程退出时,如何让子进程处于活动状态?

我想用multiprocessing模块来完成这个.

当我这样做时,像:

    $ python my_process.py
Run Code Online (Sandbox Code Playgroud)

我启动一个父进程,然后让父进程生成一个子进程,

然后我希望父进程退出,但子进程继续工作.

请允许我写一个错误的代码来解释自己:

from multiprocessing import Process

def f(x):
    with open('out.dat', 'w') as f:
        f.write(x)

if __name__ == '__main__':
    p = Process(target=f, args=('bbb',))
    p.daemon = True    # This is key, set the daemon, then parent exits itself
    p.start()

    #p.join()    # This is WRONG code, just want to exlain what I mean.
    # the child processes will be killed, when father exit
Run Code Online (Sandbox Code Playgroud)

那么,如何在父进程完成时启动一个不会被杀死的进程?


20140714

嗨,你们

我的朋友告诉我一个解决方案......

我只是想...

无论如何,只是让你看看:

import os
os.system('python your_app.py&') …
Run Code Online (Sandbox Code Playgroud)

python multiprocessing

8
推荐指数
1
解决办法
2849
查看次数

Python中的_tuple有用吗?

我阅读了collections.namedtuple今天的官方文档并_tuple__new__方法中找到了.我没有找到_tuple定义的位置.

您可以尝试在Python中运行下面的代码,它不会引发任何错误.

>>> Point = namedtuple('Point', ['x', 'y'], verbose=True)
class Point(tuple):
    'Point(x, y)'

    __slots__ = ()

    _fields = ('x', 'y')

    def __new__(_cls, x, y):
        'Create a new instance of Point(x, y)'
        return _tuple.__new__(_cls, (x, y)) # Here. Why _tuple?
Run Code Online (Sandbox Code Playgroud)

更新:有什么好处

from builtins import property as _property, tuple as _tuple
Run Code Online (Sandbox Code Playgroud)

是不是只是为了让tuple是一个受保护的价值?我对吗?

python namedtuple python-internals

5
推荐指数
1
解决办法
290
查看次数

sqlalchemy - 如何获得更新方法结果?

我执行update方法:

# connect db to get `conn`
# get metadate which is table `account`
u = account.update().where(account.c.ID == 9).values(USERNAME='k9')
r = conn.execute(u)
Run Code Online (Sandbox Code Playgroud)

我怎样才能获得update成功或失败?我检查了文档,但没有找到...

或者也许我只是不在乎这个?

r是一个ResultProxy自动update提交和关闭的操作。

谢谢:)


附加

谢谢@Lucas Kahlert 的回答,好点!

rowcount满足我的问题的情况。

文档行数

This attribute returns the number of rows matched, 
which is not necessarily the same as the number of rows that were actually modified

In [7]: u = account.update().\
        where( and_((account.c.ID == 4), (account.c.PWD =='4297f44b1'))).\
        values(PWD='hahahaha')

In [8]: print …
Run Code Online (Sandbox Code Playgroud)

sqlalchemy

5
推荐指数
1
解决办法
1万
查看次数

关键字`in`如何在迭代器中工作?

我有这个:

>>> 1 in (i for i in range(0, 5))
True

>>> 2 in (i for i in range(0, 5))
True
Run Code Online (Sandbox Code Playgroud)

之后:

>>> gen = (i for i in range(0, 5))
>>> 1 in gen
True
>>> 3 in gen
True
>>> 2 in gen
False
Run Code Online (Sandbox Code Playgroud)

关键字是否in有效:

3 in gen
Run Code Online (Sandbox Code Playgroud)

等于:

j = 3
for i in gen:
    if i == j:
        return True
    else:
        return False
Run Code Online (Sandbox Code Playgroud)

但在for循环之后,请勿再次将迭代器重置为FIRST.

是对的吗?

python iterator in-operator

2
推荐指数
1
解决办法
111
查看次数

“ spring-boot-maven-plugin”和“ maven-compiler-plugin”之间有什么区别?

我不知道之间的区别spring-boot-maven-pluginmaven-compiler-plugin

   <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
Run Code Online (Sandbox Code Playgroud)

这是否意味着Spring Boot Maven Plugin包括maven-compiler-plugin

我只是使用Spring Boot Maven Plugin就可以,不需要添加2个插件??

java spring maven-3 maven spring-boot

-2
推荐指数
2
解决办法
289
查看次数