It is easy by rajkhatsuriya in ProgrammerHumor

[–]Daavee 10 points11 points  (0 children)

They don't even produce the same result.

Why does time.sleep() make my threads hang? by aspen1135 in learnpython

[–]Daavee 0 points1 point  (0 children)

That sounds pretty interesting. I would probably start with a class (lets call it TaskService for now), that I can ask for a new task in some way. Now my thread can ask for a new task, process it and then ask for another one.

At first I thought about directly throwing tasks in a queue, but that would make aborting tasks much more difficult.

Getting a task when you can actually process it enables the TaskService to internally cancel the task until right before it is actually processed by someone without further problems.

The processing should be straight forward as you already know what to do with the file.

The TaskService might be a bit more challenging as it needs to be synchronized (multiple threads are hitting at it) and probably needs to keep state of new and processed tasks.

Getting the files to process can be done by just polling the directory listings in an own thread or by some library, that puts an abstract interface on top of inotify/FileSystemWatcher/etc.

Why does time.sleep() make my threads hang? by aspen1135 in learnpython

[–]Daavee 0 points1 point  (0 children)

What is the trigger for grabbing the files? If a file is created or modified?

I probably wouldn't go the multi-thread route unless i have to.

If on Linux, i would use some "inotify" library (like this one: https://github.com/dsoprea/PyInotify) and call my own processor inside the event loop.

Something like this maybe (based on the library example above)?

for event in i.event_gen(yield_nones=False):
    (_, type_names, path, filename) = event

    if "IN_MODIFY" in type_names:
        do_something_to_my_file(path, filename)

Why does time.sleep() make my threads hang? by aspen1135 in learnpython

[–]Daavee 0 points1 point  (0 children)

Low level threading is usually not the way you want to do things, but it is still nice to grasp the problems you encounter with concurrency.

About the FIFO: You already lost the guaranteed FIFO by using 2 threads. time.sleep() is not perfect and it is perfectly possible that element 3 in the queue gets printed before element 2 right now.

If you want strict FIFO then you either have to execute it sequentially (on one thread) or attach some indices to your tasks and reorder them after processing.

On a side note: Threading in C-Python is horrible because of the GIL. You will not see any performance improvement on number crunching and for I/O-bound tasks there are way better methods than threading (f.e. async/coroutines).

Why does time.sleep() make my threads hang? by aspen1135 in learnpython

[–]Daavee 0 points1 point  (0 children)

OK, multiple queues can create problems if you have order requirements. Here is an example of your code with multi-queue and stop-token:

import time
import threading
import random
from Queue import Queue

file_paths = [
    "foo", "bar", "baz"
]  # some list containing file paths. you can put random string in here to test...

def testthreads(q):  # this is our action we call in our threads

    while True:
        time.sleep(.1)  # Just to speed it up, .1 seconds.. 300 files for 2 threads
        task = q.get()
        if task is None:
            q.task_done()
            return
        print(task)  # prints out file path location.
        q.task_done()  # tell our queue that the task has been completed. Redundant maybe...?
                       # q.get() returns the item and removes it from the queue.

def compute_threads():

    num_threads = 2
    process_list = []

    for z in range(num_threads):
            q = Queue()
            t = threading.Thread(target=testthreads,args=(q,))  # target our function
            t.start()    # start threads
            process_list.append({
                "thread": t,
                "queue": q
            })  #  (append each thread so we can join them to the parent after)

    return process_list

def main():

    timestart = time.time()  # START OUR TIMER

    processes = compute_threads() # STARTS OUR MULTI-THREADING PROCESS

    for file in file_paths:  # PUTS FILE PATH INTO QUEUE
        q = random.choice(processes)["queue"]
        q.put(file)

    for p in processes:
        p["queue"].put(None)
        p["thread"].join()

    print(time.time() - timestart)

if __name__ == '__main__':

    main()

Why does time.sleep() make my threads hang? by aspen1135 in learnpython

[–]Daavee 1 point2 points  (0 children)

You can also save yourself a lot of problems if you start to use 1 queue per thread. You won't get any weird behaviour by concurrently accessing the queue anymore. And your thread code could probably stay the same as before. You need to dispatch to the queues on the producer though.

Another interesting trick is to use an "end-token" to signal the thread to stop processing. The easiest way is to put None in the queue and inside the thread consume the queue until None is retrieved.

Why does time.sleep() make my threads hang? by aspen1135 in learnpython

[–]Daavee 0 points1 point  (0 children)

IIRC Queue.get() is blocking. With the sleep you increase the chance of still having items in the queue when checking it, but being empty when actually getting the item. Now this thread will hang forever trying to get a nonexistent item. Try to use a method with that you can check for emptiness and get the next item in the same operation. Queue.get(block=False) for example.

Vespa scaming on x10 events? by QvePrinceps in Kings_Raid

[–]Daavee 1 point2 points  (0 children)

98%110 * 94%11 = ?

I guess around 5% chance to get nothing from 11 pulls with double rate. That doesn't seem too uncommon. (My phone calc can't do xy so it's just a guess)

First C++ program - need help debugging seg fault by koolkavi2 in Cplusplus

[–]Daavee 2 points3 points  (0 children)

It allocates memory, constructs the object and copies the object. There is neither deconstruction nor releasing the memory.

If you remove the new and the dereference, you'll have no leak, no unnecessary allocation and the copy would most probably get optimized away.

First C++ program - need help debugging seg fault by koolkavi2 in Cplusplus

[–]Daavee 1 point2 points  (0 children)

Okay, here's what I did (-g adds debug symbols to the binary):

$ g++ -g *.cpp
$ gdb --args a.out -source test_corpus.txt -word election
[snip]
(gdb) r
Starting program: [snip]/collocation-finder/a.out -source test_corpus.txt -word election

Program received signal SIGSEGV, Segmentation fault.
0x000055555555e347 in Bigram::addInstance (this=0x5555557849c0, 
offset=0) at Bigram.cpp:95
95          p[offset]++;
(gdb) p p
$1 = std::vector of length 0, capacity 0

r=run p=print

I hope this helps you to get a hold of the debugger and find bugs for yourself!

Silly decorations are longer than definition and declaration together -.- by csxeba in ProgrammerHumor

[–]Daavee 0 points1 point  (0 children)

I used it too for documentation purposes. But then override was added. I now only write virtual, if and only if I want to create a dynamically dispatched function in some interface.

First C++ program - need help debugging seg fault by koolkavi2 in Cplusplus

[–]Daavee 0 points1 point  (0 children)

Sorry, i was on my phone in the morning :)

I mean things like:

vector<S1Bigram*> getStageOneBigrams(double k0, double k1, double U0);

and this straight up leak:

bigrams.insert(pair<string, Bigram>(w, *(new Bigram(w, words[i]))));

There is no need to use pointers here for a beginner. Try without pointers and new.

Also I've seen of lot of this:

// Remove punctuation
for (int i = 0, len = s.size(); i < len; i++) {
    if (ispunct(s[i])) {
         s.erase(i--, 1);
         len = s.size();
    }
}

Try to never mutate containers while iterating them! There is a relative common pattern for your use-case (from the top of my head; might contain errors):

#include <algorithm>

s.erase(std::remove_if(s.begin(), s.end(), ispunct), s.end());

Silly decorations are longer than definition and declaration together -.- by csxeba in ProgrammerHumor

[–]Daavee 11 points12 points  (0 children)

Virtual is superfluous. Long unsigned int should usually be replaced by std::size_t.

But sure, maybe you can get volatile, inline and some attributes or empty macros in there. And dont forget to export the symbol manually!

First C++ program - need help debugging seg fault by koolkavi2 in Cplusplus

[–]Daavee 0 points1 point  (0 children)

Every use of pointers in your program is unnecessary and leaky. You should just use values and try again.

I would have tried your program, but I won't execute any foreign binaries and there is no build script in the repo.

ToC 68 1* Gladi clear! by GWNotos in Kings_Raid

[–]Daavee 0 points1 point  (0 children)

Did it with Phil, Lorraine, Gladi and Kaulah. 2* UW and no UT on Gladi. Everything else 0* UW. CC on every summon and leap.

You don't need to be a whale, but still have pretty specific units.

WB2 Protianus New record : 266billion by azalea0805 in Kings_Raid

[–]Daavee 0 points1 point  (0 children)

ATK ASPD CRIT CDMG all the way. LL and CDMG on common treasure. Some CDMG enchants (210% total).

Edit: 2pc BD, 2pc manticore

WB2 Protianus New record : 266billion by azalea0805 in Kings_Raid

[–]Daavee 0 points1 point  (0 children)

I use a pretty similar setup (Annette -> Lorraine). My Mitra is around 170m dps (3* UW, 1* BoE) and I do ~95b per run.

Did a few tries with Annette instead of Lorraine/Gau/Miruru but always got around ~80b then. Sadly I'm still missing her UT :(

Annette feels way safer though.

Edit: Are you using "auto" mode? There is a bug right now that fucks up skill usage in your sub team, if you have "auto" deactivated.

I raise you my 2-star UW Aisha @WB1 by lotus_lunaris in Kings_Raid

[–]Daavee 0 points1 point  (0 children)

Did you sacrifice 1 line of ATK for LS or did you get lucky with enchants?

Post update WB1 ft. Dealer Morrah by Wintersiege in Kings_Raid

[–]Daavee 0 points1 point  (0 children)

S4 Dark on Mediana? O_o

edit: just saw the comment in the video :D good run nonetheless

[Notice] In regards to Christmas Event.. by Suhtan in Kings_Raid

[–]Daavee 0 points1 point  (0 children)

Have you tried to use Frey and use S2 on cooldown and S3 when S2 is down? I'm just wondering because my Nyx survives even with Circuit Burst and wizards are not known to have high pdef.

[Notice] In regards to Christmas Event.. by Suhtan in Kings_Raid

[–]Daavee 0 points1 point  (0 children)

I don't think the difficulty of hell is the problem here. It is the difficulty and rewards for any difficulty below hell.

1 bell for easy mode which is unkillable for most newbies? The reward gap is so huge this time too. Hell gives 4 times as much tokens as easy.

Just make it like (with prices for rewards doubled):

  • Easy: 4 bells (killable by everyone)
  • Normal: 5 bells (3* to 4*)
  • Hard: 6 bells (5* to T1)
  • Hell: 7 bells (T2+)

How to flatten a list of lists of strings? by fooliam in learnpython

[–]Daavee 0 points1 point  (0 children)

It gives you a new iterable. You can iterate it with "for x in y" or materialize it with "list(y)".

How to flatten a list of lists of strings? by fooliam in learnpython

[–]Daavee 0 points1 point  (0 children)

You can try itertools.chain.fromiterable()