小编Luc*_*can的帖子

python中线程之间的数据通信

我是 python 的新手,我对 python 中的线程知之甚少。这是我的示例代码。

import threading
from threading import Thread
import time

check = False

def func1():
    print ("funn1 started")
    while check:
        print ("got permission")

def func2():
    global check
    print ("func2 started")
    time.sleep(2)
    check = True
    time.sleep(2)
    check = False

if __name__ == '__main__':
    Thread(target = func1).start()
    Thread(target = func2).start()
Run Code Online (Sandbox Code Playgroud)

我想要的是看到“获得许可”作为输出。但是使用我当前的代码,它不会发生。我假设func1线程在func2check值更改为True.

我怎样才能func1活下去?我在互联网上进行了研究,但找不到解决方案。任何帮助,将不胜感激。先感谢您!

python python-multithreading

6
推荐指数
2
解决办法
8946
查看次数

我可以让这个 switch 语句更小吗?最好有 for 循环

有没有办法让它更小,我尝试使用 for 循环,但无法为每个可能的类型创建一个随机实例。

Random randFireworkEffect = new Random(5);
switch(randFireworkEffect.nextInt()) {
    case 0:
        e = FireworkEffect.builder().flicker(true).withColor(c).withFade(c).with(Type.BALL).trail(true).build();
        break;
    case 1:
        e = FireworkEffect.builder().flicker(true).withColor(c).withFade(c).with(Type.BALL_LARGE).trail(true).build();
        break;
    case 2:
         e = FireworkEffect.builder().flicker(true).withColor(c).withFade(c).with(Type.BURST).trail(true).build();
        break;
    case 3:
         e = FireworkEffect.builder().flicker(true).withColor(c).withFade(c).with(Type.CREEPER).trail(true).build();
        break;
    case 4:
         e = FireworkEffect.builder().flicker(true).withColor(c).withFade(c).with(Type.STAR).trail(true).build();
        break;
}
Run Code Online (Sandbox Code Playgroud)

java bukkit

-1
推荐指数
1
解决办法
112
查看次数

有没有办法在Python中用括号替换字符串中的所有大括号?

我目前正在开发一个涉及包含大括号的字符串的项目,但我想用括号替换大括号。我尝试过使用 .replace() 函数,但它似乎无法将大括号识别为普通字符。

我得到的字符串示例如下:

{AttributeToModify=AttributeDefinition'D_Attributes.Weapon.WeaponClipSize',ModifierType=MT_Scale,BaseModifierValue={BaseValueConstant=2.000000,BaseValueAttribute=无,InitializationDefinition=无,BaseValueScaleConstant=1.000000}}

期望的结果如下:

(AttributeToModify=AttributeDefinition'D_Attributes.Weapon.WeaponClipSize',ModifierType=MT_Scale,BaseModifierValue=(BaseValueConstant=2.000000,BaseValueAttribute=无,InitializationDefinition=无,BaseValueScaleConstant=1.000000))

python

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

标签 统计

python ×2

bukkit ×1

java ×1

python-multithreading ×1